xref: /aosp_15_r20/external/swiftshader/src/Pipeline/PixelProgram.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
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 "PixelProgram.hpp"
16*03ce13f7SAndroid Build Coastguard Worker 
17*03ce13f7SAndroid Build Coastguard Worker #include "Constants.hpp"
18*03ce13f7SAndroid Build Coastguard Worker #include "SamplerCore.hpp"
19*03ce13f7SAndroid Build Coastguard Worker #include "Device/Primitive.hpp"
20*03ce13f7SAndroid Build Coastguard Worker #include "Device/Renderer.hpp"
21*03ce13f7SAndroid Build Coastguard Worker #include "Vulkan/VkDevice.hpp"
22*03ce13f7SAndroid Build Coastguard Worker 
23*03ce13f7SAndroid Build Coastguard Worker namespace sw {
24*03ce13f7SAndroid Build Coastguard Worker 
PixelProgram(const PixelProcessor::State & state,const vk::PipelineLayout * pipelineLayout,const SpirvShader * spirvShader,const vk::Attachments & attachments,const vk::DescriptorSet::Bindings & descriptorSets)25*03ce13f7SAndroid Build Coastguard Worker PixelProgram::PixelProgram(
26*03ce13f7SAndroid Build Coastguard Worker     const PixelProcessor::State &state,
27*03ce13f7SAndroid Build Coastguard Worker     const vk::PipelineLayout *pipelineLayout,
28*03ce13f7SAndroid Build Coastguard Worker     const SpirvShader *spirvShader,
29*03ce13f7SAndroid Build Coastguard Worker     const vk::Attachments &attachments,
30*03ce13f7SAndroid Build Coastguard Worker     const vk::DescriptorSet::Bindings &descriptorSets)
31*03ce13f7SAndroid Build Coastguard Worker     : PixelRoutine(state, pipelineLayout, spirvShader, attachments, descriptorSets)
32*03ce13f7SAndroid Build Coastguard Worker {
33*03ce13f7SAndroid Build Coastguard Worker }
34*03ce13f7SAndroid Build Coastguard Worker 
35*03ce13f7SAndroid Build Coastguard Worker // Union all cMask and return it as Booleans
maskAny(Int cMask[4],const SampleSet & samples)36*03ce13f7SAndroid Build Coastguard Worker SIMD::Int PixelProgram::maskAny(Int cMask[4], const SampleSet &samples)
37*03ce13f7SAndroid Build Coastguard Worker {
38*03ce13f7SAndroid Build Coastguard Worker 	// See if at least 1 sample is used
39*03ce13f7SAndroid Build Coastguard Worker 	Int maskUnion = 0;
40*03ce13f7SAndroid Build Coastguard Worker 	for(unsigned int q : samples)
41*03ce13f7SAndroid Build Coastguard Worker 	{
42*03ce13f7SAndroid Build Coastguard Worker 		maskUnion |= cMask[q];
43*03ce13f7SAndroid Build Coastguard Worker 	}
44*03ce13f7SAndroid Build Coastguard Worker 
45*03ce13f7SAndroid Build Coastguard Worker 	// Convert to Booleans
46*03ce13f7SAndroid Build Coastguard Worker 	SIMD::Int laneBits = SIMD::Int([](int i) { return 1 << i; });  // 1, 2, 4, 8, ...
47*03ce13f7SAndroid Build Coastguard Worker 	SIMD::Int mask(maskUnion);
48*03ce13f7SAndroid Build Coastguard Worker 	mask = CmpNEQ(mask & laneBits, 0);
49*03ce13f7SAndroid Build Coastguard Worker 	return mask;
50*03ce13f7SAndroid Build Coastguard Worker }
51*03ce13f7SAndroid Build Coastguard Worker 
52*03ce13f7SAndroid Build Coastguard Worker // Union all cMask/sMask/zMask and return it as Booleans
maskAny(Int cMask[4],Int sMask[4],Int zMask[4],const SampleSet & samples)53*03ce13f7SAndroid Build Coastguard Worker SIMD::Int PixelProgram::maskAny(Int cMask[4], Int sMask[4], Int zMask[4], const SampleSet &samples)
54*03ce13f7SAndroid Build Coastguard Worker {
55*03ce13f7SAndroid Build Coastguard Worker 	// See if at least 1 sample is used
56*03ce13f7SAndroid Build Coastguard Worker 	Int maskUnion = 0;
57*03ce13f7SAndroid Build Coastguard Worker 	for(unsigned int q : samples)
58*03ce13f7SAndroid Build Coastguard Worker 	{
59*03ce13f7SAndroid Build Coastguard Worker 		maskUnion |= (cMask[q] & sMask[q] & zMask[q]);
60*03ce13f7SAndroid Build Coastguard Worker 	}
61*03ce13f7SAndroid Build Coastguard Worker 
62*03ce13f7SAndroid Build Coastguard Worker 	// Convert to Booleans
63*03ce13f7SAndroid Build Coastguard Worker 	SIMD::Int laneBits = SIMD::Int([](int i) { return 1 << i; });  // 1, 2, 4, 8, ...
64*03ce13f7SAndroid Build Coastguard Worker 	SIMD::Int mask(maskUnion);
65*03ce13f7SAndroid Build Coastguard Worker 	mask = CmpNEQ(mask & laneBits, 0);
66*03ce13f7SAndroid Build Coastguard Worker 	return mask;
67*03ce13f7SAndroid Build Coastguard Worker }
68*03ce13f7SAndroid Build Coastguard Worker 
setBuiltins(Int & x,Int & y,SIMD::Float (& z)[4],SIMD::Float & w,Int cMask[4],const SampleSet & samples)69*03ce13f7SAndroid Build Coastguard Worker void PixelProgram::setBuiltins(Int &x, Int &y, SIMD::Float (&z)[4], SIMD::Float &w, Int cMask[4], const SampleSet &samples)
70*03ce13f7SAndroid Build Coastguard Worker {
71*03ce13f7SAndroid Build Coastguard Worker 	routine.setImmutableInputBuiltins(spirvShader);
72*03ce13f7SAndroid Build Coastguard Worker 
73*03ce13f7SAndroid Build Coastguard Worker 	// TODO(b/146486064): Consider only assigning these to the SpirvRoutine iff
74*03ce13f7SAndroid Build Coastguard Worker 	// they are ever going to be read.
75*03ce13f7SAndroid Build Coastguard Worker 	float x0 = 0.5f;
76*03ce13f7SAndroid Build Coastguard Worker 	float y0 = 0.5f;
77*03ce13f7SAndroid Build Coastguard Worker 	float x1 = 1.5f;
78*03ce13f7SAndroid Build Coastguard Worker 	float y1 = 1.5f;
79*03ce13f7SAndroid Build Coastguard Worker 
80*03ce13f7SAndroid Build Coastguard Worker 	// "When Sample Shading is enabled, the x and y components of FragCoord reflect the
81*03ce13f7SAndroid Build Coastguard Worker 	//  location of one of the samples corresponding to the shader invocation. Otherwise,
82*03ce13f7SAndroid Build Coastguard Worker 	//  the x and y components of FragCoord reflect the location of the center of the fragment."
83*03ce13f7SAndroid Build Coastguard Worker 	if(state.sampleShadingEnabled && state.multiSampleCount > 1)
84*03ce13f7SAndroid Build Coastguard Worker 	{
85*03ce13f7SAndroid Build Coastguard Worker 		x0 = VkSampleLocations4[samples[0]][0];
86*03ce13f7SAndroid Build Coastguard Worker 		y0 = VkSampleLocations4[samples[0]][1];
87*03ce13f7SAndroid Build Coastguard Worker 		x1 = 1.0f + x0;
88*03ce13f7SAndroid Build Coastguard Worker 		y1 = 1.0f + y0;
89*03ce13f7SAndroid Build Coastguard Worker 	}
90*03ce13f7SAndroid Build Coastguard Worker 
91*03ce13f7SAndroid Build Coastguard Worker 	routine.fragCoord[0] = SIMD::Float(Float(x)) + SIMD::Float(x0, x1, x0, x1);
92*03ce13f7SAndroid Build Coastguard Worker 	routine.fragCoord[1] = SIMD::Float(Float(y)) + SIMD::Float(y0, y0, y1, y1);
93*03ce13f7SAndroid Build Coastguard Worker 	routine.fragCoord[2] = z[0];  // sample 0
94*03ce13f7SAndroid Build Coastguard Worker 	routine.fragCoord[3] = w;
95*03ce13f7SAndroid Build Coastguard Worker 
96*03ce13f7SAndroid Build Coastguard Worker 	routine.invocationsPerSubgroup = SIMD::Width;
97*03ce13f7SAndroid Build Coastguard Worker 	routine.helperInvocation = ~maskAny(cMask, samples);
98*03ce13f7SAndroid Build Coastguard Worker 	routine.windowSpacePosition[0] = SIMD::Int(x) + SIMD::Int(0, 1, 0, 1);
99*03ce13f7SAndroid Build Coastguard Worker 	routine.windowSpacePosition[1] = SIMD::Int(y) + SIMD::Int(0, 0, 1, 1);
100*03ce13f7SAndroid Build Coastguard Worker 	routine.layer = *Pointer<Int>(data + OFFSET(DrawData, layer));
101*03ce13f7SAndroid Build Coastguard Worker 
102*03ce13f7SAndroid Build Coastguard Worker 	// PointCoord formula reference: https://www.khronos.org/registry/vulkan/specs/1.2/html/vkspec.html#primsrast-points-basic
103*03ce13f7SAndroid Build Coastguard Worker 	// Note we don't add a 0.5 offset to x and y here (like for fragCoord) because pointCoordX/Y have 0.5 subtracted as part of the viewport transform.
104*03ce13f7SAndroid Build Coastguard Worker 	SIMD::Float pointSizeInv = SIMD::Float(*Pointer<Float>(primitive + OFFSET(Primitive, pointSizeInv)));
105*03ce13f7SAndroid Build Coastguard Worker 	routine.pointCoord[0] = SIMD::Float(0.5f) + pointSizeInv * (((SIMD::Float(Float(x)) + SIMD::Float(0.0f, 1.0f, 0.0f, 1.0f)) - SIMD::Float(*Pointer<Float>(primitive + OFFSET(Primitive, x0)))));
106*03ce13f7SAndroid Build Coastguard Worker 	routine.pointCoord[1] = SIMD::Float(0.5f) + pointSizeInv * (((SIMD::Float(Float(y)) + SIMD::Float(0.0f, 0.0f, 1.0f, 1.0f)) - SIMD::Float(*Pointer<Float>(primitive + OFFSET(Primitive, y0)))));
107*03ce13f7SAndroid Build Coastguard Worker 
108*03ce13f7SAndroid Build Coastguard Worker 	routine.setInputBuiltin(spirvShader, spv::BuiltInViewIndex, [&](const Spirv::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
109*03ce13f7SAndroid Build Coastguard Worker 		assert(builtin.SizeInComponents == 1);
110*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(routine.layer));
111*03ce13f7SAndroid Build Coastguard Worker 	});
112*03ce13f7SAndroid Build Coastguard Worker 
113*03ce13f7SAndroid Build Coastguard Worker 	routine.setInputBuiltin(spirvShader, spv::BuiltInFragCoord, [&](const Spirv::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
114*03ce13f7SAndroid Build Coastguard Worker 		assert(builtin.SizeInComponents == 4);
115*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent + 0] = routine.fragCoord[0];
116*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent + 1] = routine.fragCoord[1];
117*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent + 2] = routine.fragCoord[2];
118*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent + 3] = routine.fragCoord[3];
119*03ce13f7SAndroid Build Coastguard Worker 	});
120*03ce13f7SAndroid Build Coastguard Worker 
121*03ce13f7SAndroid Build Coastguard Worker 	routine.setInputBuiltin(spirvShader, spv::BuiltInPointCoord, [&](const Spirv::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
122*03ce13f7SAndroid Build Coastguard Worker 		assert(builtin.SizeInComponents == 2);
123*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent + 0] = routine.pointCoord[0];
124*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent + 1] = routine.pointCoord[1];
125*03ce13f7SAndroid Build Coastguard Worker 	});
126*03ce13f7SAndroid Build Coastguard Worker 
127*03ce13f7SAndroid Build Coastguard Worker 	routine.setInputBuiltin(spirvShader, spv::BuiltInSubgroupSize, [&](const Spirv::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
128*03ce13f7SAndroid Build Coastguard Worker 		assert(builtin.SizeInComponents == 1);
129*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(SIMD::Width));
130*03ce13f7SAndroid Build Coastguard Worker 	});
131*03ce13f7SAndroid Build Coastguard Worker 
132*03ce13f7SAndroid Build Coastguard Worker 	routine.setInputBuiltin(spirvShader, spv::BuiltInHelperInvocation, [&](const Spirv::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
133*03ce13f7SAndroid Build Coastguard Worker 		assert(builtin.SizeInComponents == 1);
134*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent] = As<SIMD::Float>(routine.helperInvocation);
135*03ce13f7SAndroid Build Coastguard Worker 	});
136*03ce13f7SAndroid Build Coastguard Worker }
137*03ce13f7SAndroid Build Coastguard Worker 
executeShader(Int cMask[4],Int sMask[4],Int zMask[4],const SampleSet & samples)138*03ce13f7SAndroid Build Coastguard Worker void PixelProgram::executeShader(Int cMask[4], Int sMask[4], Int zMask[4], const SampleSet &samples)
139*03ce13f7SAndroid Build Coastguard Worker {
140*03ce13f7SAndroid Build Coastguard Worker 	routine.device = device;
141*03ce13f7SAndroid Build Coastguard Worker 	routine.descriptorSets = data + OFFSET(DrawData, descriptorSets);
142*03ce13f7SAndroid Build Coastguard Worker 	routine.descriptorDynamicOffsets = data + OFFSET(DrawData, descriptorDynamicOffsets);
143*03ce13f7SAndroid Build Coastguard Worker 	routine.pushConstants = data + OFFSET(DrawData, pushConstants);
144*03ce13f7SAndroid Build Coastguard Worker 	routine.constants = device + OFFSET(vk::Device, constants);
145*03ce13f7SAndroid Build Coastguard Worker 
146*03ce13f7SAndroid Build Coastguard Worker 	auto it = spirvShader->inputBuiltins.find(spv::BuiltInFrontFacing);
147*03ce13f7SAndroid Build Coastguard Worker 	if(it != spirvShader->inputBuiltins.end())
148*03ce13f7SAndroid Build Coastguard Worker 	{
149*03ce13f7SAndroid Build Coastguard Worker 		ASSERT(it->second.SizeInComponents == 1);
150*03ce13f7SAndroid Build Coastguard Worker 		auto frontFacing = SIMD::Int(*Pointer<Int>(primitive + OFFSET(Primitive, clockwiseMask)));
151*03ce13f7SAndroid Build Coastguard Worker 		routine.getVariable(it->second.Id)[it->second.FirstComponent] = As<SIMD::Float>(frontFacing);
152*03ce13f7SAndroid Build Coastguard Worker 	}
153*03ce13f7SAndroid Build Coastguard Worker 
154*03ce13f7SAndroid Build Coastguard Worker 	it = spirvShader->inputBuiltins.find(spv::BuiltInSampleMask);
155*03ce13f7SAndroid Build Coastguard Worker 	if(it != spirvShader->inputBuiltins.end())
156*03ce13f7SAndroid Build Coastguard Worker 	{
157*03ce13f7SAndroid Build Coastguard Worker 		ASSERT(SIMD::Width == 4);
158*03ce13f7SAndroid Build Coastguard Worker 		SIMD::Int laneBits = SIMD::Int(1, 2, 4, 8);
159*03ce13f7SAndroid Build Coastguard Worker 
160*03ce13f7SAndroid Build Coastguard Worker 		SIMD::Int inputSampleMask = 0;
161*03ce13f7SAndroid Build Coastguard Worker 		for(unsigned int q : samples)
162*03ce13f7SAndroid Build Coastguard Worker 		{
163*03ce13f7SAndroid Build Coastguard Worker 			inputSampleMask |= SIMD::Int(1 << q) & CmpNEQ(SIMD::Int(cMask[q]) & laneBits, 0);
164*03ce13f7SAndroid Build Coastguard Worker 		}
165*03ce13f7SAndroid Build Coastguard Worker 
166*03ce13f7SAndroid Build Coastguard Worker 		routine.getVariable(it->second.Id)[it->second.FirstComponent] = As<SIMD::Float>(inputSampleMask);
167*03ce13f7SAndroid Build Coastguard Worker 		// Sample mask input is an array, as the spec contemplates MSAA levels higher than 32.
168*03ce13f7SAndroid Build Coastguard Worker 		// Fill any non-zero indices with 0.
169*03ce13f7SAndroid Build Coastguard Worker 		for(auto i = 1u; i < it->second.SizeInComponents; i++)
170*03ce13f7SAndroid Build Coastguard Worker 		{
171*03ce13f7SAndroid Build Coastguard Worker 			routine.getVariable(it->second.Id)[it->second.FirstComponent + i] = 0;
172*03ce13f7SAndroid Build Coastguard Worker 		}
173*03ce13f7SAndroid Build Coastguard Worker 	}
174*03ce13f7SAndroid Build Coastguard Worker 
175*03ce13f7SAndroid Build Coastguard Worker 	it = spirvShader->inputBuiltins.find(spv::BuiltInSampleId);
176*03ce13f7SAndroid Build Coastguard Worker 	if(it != spirvShader->inputBuiltins.end())
177*03ce13f7SAndroid Build Coastguard Worker 	{
178*03ce13f7SAndroid Build Coastguard Worker 		ASSERT(samples.size() == 1);
179*03ce13f7SAndroid Build Coastguard Worker 		int sampleId = samples[0];
180*03ce13f7SAndroid Build Coastguard Worker 		routine.getVariable(it->second.Id)[it->second.FirstComponent] =
181*03ce13f7SAndroid Build Coastguard Worker 		    As<SIMD::Float>(SIMD::Int(sampleId));
182*03ce13f7SAndroid Build Coastguard Worker 	}
183*03ce13f7SAndroid Build Coastguard Worker 
184*03ce13f7SAndroid Build Coastguard Worker 	it = spirvShader->inputBuiltins.find(spv::BuiltInSamplePosition);
185*03ce13f7SAndroid Build Coastguard Worker 	if(it != spirvShader->inputBuiltins.end())
186*03ce13f7SAndroid Build Coastguard Worker 	{
187*03ce13f7SAndroid Build Coastguard Worker 		ASSERT(samples.size() == 1);
188*03ce13f7SAndroid Build Coastguard Worker 		int sampleId = samples[0];
189*03ce13f7SAndroid Build Coastguard Worker 		routine.getVariable(it->second.Id)[it->second.FirstComponent + 0] =
190*03ce13f7SAndroid Build Coastguard Worker 		    SIMD::Float((state.multiSampleCount > 1) ? VkSampleLocations4[sampleId][0] : 0.5f);
191*03ce13f7SAndroid Build Coastguard Worker 		routine.getVariable(it->second.Id)[it->second.FirstComponent + 1] =
192*03ce13f7SAndroid Build Coastguard Worker 		    SIMD::Float((state.multiSampleCount > 1) ? VkSampleLocations4[sampleId][1] : 0.5f);
193*03ce13f7SAndroid Build Coastguard Worker 	}
194*03ce13f7SAndroid Build Coastguard Worker 
195*03ce13f7SAndroid Build Coastguard Worker 	// Note: all lanes initially active to facilitate derivatives etc. Actual coverage is
196*03ce13f7SAndroid Build Coastguard Worker 	// handled separately, through the cMask.
197*03ce13f7SAndroid Build Coastguard Worker 	SIMD::Int activeLaneMask = 0xFFFFFFFF;
198*03ce13f7SAndroid Build Coastguard Worker 	SIMD::Int storesAndAtomicsMask = maskAny(cMask, sMask, zMask, samples);
199*03ce13f7SAndroid Build Coastguard Worker 	routine.discardMask = 0;
200*03ce13f7SAndroid Build Coastguard Worker 
201*03ce13f7SAndroid Build Coastguard Worker 	spirvShader->emit(&routine, activeLaneMask, storesAndAtomicsMask, descriptorSets, &attachments, state.multiSampleCount);
202*03ce13f7SAndroid Build Coastguard Worker 	spirvShader->emitEpilog(&routine);
203*03ce13f7SAndroid Build Coastguard Worker 
204*03ce13f7SAndroid Build Coastguard Worker 	for(int i = 0; i < MAX_COLOR_BUFFERS; i++)
205*03ce13f7SAndroid Build Coastguard Worker 	{
206*03ce13f7SAndroid Build Coastguard Worker 		c[i].x = routine.outputs[i * 4 + 0];
207*03ce13f7SAndroid Build Coastguard Worker 		c[i].y = routine.outputs[i * 4 + 1];
208*03ce13f7SAndroid Build Coastguard Worker 		c[i].z = routine.outputs[i * 4 + 2];
209*03ce13f7SAndroid Build Coastguard Worker 		c[i].w = routine.outputs[i * 4 + 3];
210*03ce13f7SAndroid Build Coastguard Worker 	}
211*03ce13f7SAndroid Build Coastguard Worker 
212*03ce13f7SAndroid Build Coastguard Worker 	clampColor(c);
213*03ce13f7SAndroid Build Coastguard Worker 
214*03ce13f7SAndroid Build Coastguard Worker 	if(spirvShader->getAnalysis().ContainsDiscard)
215*03ce13f7SAndroid Build Coastguard Worker 	{
216*03ce13f7SAndroid Build Coastguard Worker 		for(unsigned int q : samples)
217*03ce13f7SAndroid Build Coastguard Worker 		{
218*03ce13f7SAndroid Build Coastguard Worker 			cMask[q] &= ~routine.discardMask;
219*03ce13f7SAndroid Build Coastguard Worker 		}
220*03ce13f7SAndroid Build Coastguard Worker 	}
221*03ce13f7SAndroid Build Coastguard Worker 
222*03ce13f7SAndroid Build Coastguard Worker 	it = spirvShader->outputBuiltins.find(spv::BuiltInSampleMask);
223*03ce13f7SAndroid Build Coastguard Worker 	if(it != spirvShader->outputBuiltins.end())
224*03ce13f7SAndroid Build Coastguard Worker 	{
225*03ce13f7SAndroid Build Coastguard Worker 		auto outputSampleMask = As<SIMD::Int>(routine.getVariable(it->second.Id)[it->second.FirstComponent]);
226*03ce13f7SAndroid Build Coastguard Worker 
227*03ce13f7SAndroid Build Coastguard Worker 		for(unsigned int q : samples)
228*03ce13f7SAndroid Build Coastguard Worker 		{
229*03ce13f7SAndroid Build Coastguard Worker 			cMask[q] &= SignMask(CmpNEQ(outputSampleMask & SIMD::Int(1 << q), SIMD::Int(0)));
230*03ce13f7SAndroid Build Coastguard Worker 		}
231*03ce13f7SAndroid Build Coastguard Worker 	}
232*03ce13f7SAndroid Build Coastguard Worker 
233*03ce13f7SAndroid Build Coastguard Worker 	it = spirvShader->outputBuiltins.find(spv::BuiltInFragDepth);
234*03ce13f7SAndroid Build Coastguard Worker 	if(it != spirvShader->outputBuiltins.end())
235*03ce13f7SAndroid Build Coastguard Worker 	{
236*03ce13f7SAndroid Build Coastguard Worker 		for(unsigned int q : samples)
237*03ce13f7SAndroid Build Coastguard Worker 		{
238*03ce13f7SAndroid Build Coastguard Worker 			z[q] = routine.getVariable(it->second.Id)[it->second.FirstComponent];
239*03ce13f7SAndroid Build Coastguard Worker 		}
240*03ce13f7SAndroid Build Coastguard Worker 	}
241*03ce13f7SAndroid Build Coastguard Worker }
242*03ce13f7SAndroid Build Coastguard Worker 
alphaTest(Int cMask[4],const SampleSet & samples)243*03ce13f7SAndroid Build Coastguard Worker Bool PixelProgram::alphaTest(Int cMask[4], const SampleSet &samples)
244*03ce13f7SAndroid Build Coastguard Worker {
245*03ce13f7SAndroid Build Coastguard Worker 	if(!state.alphaToCoverage)
246*03ce13f7SAndroid Build Coastguard Worker 	{
247*03ce13f7SAndroid Build Coastguard Worker 		return true;
248*03ce13f7SAndroid Build Coastguard Worker 	}
249*03ce13f7SAndroid Build Coastguard Worker 
250*03ce13f7SAndroid Build Coastguard Worker 	alphaToCoverage(cMask, c[0].w, samples);
251*03ce13f7SAndroid Build Coastguard Worker 
252*03ce13f7SAndroid Build Coastguard Worker 	Int pass = 0;
253*03ce13f7SAndroid Build Coastguard Worker 	for(unsigned int q : samples)
254*03ce13f7SAndroid Build Coastguard Worker 	{
255*03ce13f7SAndroid Build Coastguard Worker 		pass = pass | cMask[q];
256*03ce13f7SAndroid Build Coastguard Worker 	}
257*03ce13f7SAndroid Build Coastguard Worker 
258*03ce13f7SAndroid Build Coastguard Worker 	return pass != 0x0;
259*03ce13f7SAndroid Build Coastguard Worker }
260*03ce13f7SAndroid Build Coastguard Worker 
blendColor(Pointer<Byte> cBuffer[4],Int & x,Int sMask[4],Int zMask[4],Int cMask[4],const SampleSet & samples)261*03ce13f7SAndroid Build Coastguard Worker void PixelProgram::blendColor(Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4], const SampleSet &samples)
262*03ce13f7SAndroid Build Coastguard Worker {
263*03ce13f7SAndroid Build Coastguard Worker 	for(int index = 0; index < MAX_COLOR_BUFFERS; index++)
264*03ce13f7SAndroid Build Coastguard Worker 	{
265*03ce13f7SAndroid Build Coastguard Worker 		if(!state.colorWriteActive(index))
266*03ce13f7SAndroid Build Coastguard Worker 		{
267*03ce13f7SAndroid Build Coastguard Worker 			continue;
268*03ce13f7SAndroid Build Coastguard Worker 		}
269*03ce13f7SAndroid Build Coastguard Worker 
270*03ce13f7SAndroid Build Coastguard Worker 		for(unsigned int q : samples)
271*03ce13f7SAndroid Build Coastguard Worker 		{
272*03ce13f7SAndroid Build Coastguard Worker 			Pointer<Byte> buffer = cBuffer[index] + q * *Pointer<Int>(data + OFFSET(DrawData, colorSliceB[index]));
273*03ce13f7SAndroid Build Coastguard Worker 
274*03ce13f7SAndroid Build Coastguard Worker 			SIMD::Float4 C = alphaBlend(index, buffer, c[index], x);
275*03ce13f7SAndroid Build Coastguard Worker 			ASSERT(SIMD::Width == 4);
276*03ce13f7SAndroid Build Coastguard Worker 			Vector4f color;
277*03ce13f7SAndroid Build Coastguard Worker 			color.x = Extract128(C.x, 0);
278*03ce13f7SAndroid Build Coastguard Worker 			color.y = Extract128(C.y, 0);
279*03ce13f7SAndroid Build Coastguard Worker 			color.z = Extract128(C.z, 0);
280*03ce13f7SAndroid Build Coastguard Worker 			color.w = Extract128(C.w, 0);
281*03ce13f7SAndroid Build Coastguard Worker 			writeColor(index, buffer, x, color, sMask[q], zMask[q], cMask[q]);
282*03ce13f7SAndroid Build Coastguard Worker 		}
283*03ce13f7SAndroid Build Coastguard Worker 	}
284*03ce13f7SAndroid Build Coastguard Worker }
285*03ce13f7SAndroid Build Coastguard Worker 
clampColor(SIMD::Float4 color[MAX_COLOR_BUFFERS])286*03ce13f7SAndroid Build Coastguard Worker void PixelProgram::clampColor(SIMD::Float4 color[MAX_COLOR_BUFFERS])
287*03ce13f7SAndroid Build Coastguard Worker {
288*03ce13f7SAndroid Build Coastguard Worker 	// "If the color attachment is fixed-point, the components of the source and destination values and blend factors
289*03ce13f7SAndroid Build Coastguard Worker 	//  are each clamped to [0,1] or [-1,1] respectively for an unsigned normalized or signed normalized color attachment
290*03ce13f7SAndroid Build Coastguard Worker 	//  prior to evaluating the blend operations. If the color attachment is floating-point, no clamping occurs."
291*03ce13f7SAndroid Build Coastguard Worker 
292*03ce13f7SAndroid Build Coastguard Worker 	for(int index = 0; index < MAX_COLOR_BUFFERS; index++)
293*03ce13f7SAndroid Build Coastguard Worker 	{
294*03ce13f7SAndroid Build Coastguard Worker 		if(!state.colorWriteActive(index) && !(index == 0 && state.alphaToCoverage))
295*03ce13f7SAndroid Build Coastguard Worker 		{
296*03ce13f7SAndroid Build Coastguard Worker 			continue;
297*03ce13f7SAndroid Build Coastguard Worker 		}
298*03ce13f7SAndroid Build Coastguard Worker 
299*03ce13f7SAndroid Build Coastguard Worker 		switch(state.colorFormat[index])
300*03ce13f7SAndroid Build Coastguard Worker 		{
301*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_UNDEFINED:
302*03ce13f7SAndroid Build Coastguard Worker 			break;
303*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
304*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
305*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A4R4G4B4_UNORM_PACK16:
306*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A4B4G4R4_UNORM_PACK16:
307*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_B5G6R5_UNORM_PACK16:
308*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R5G5B5A1_UNORM_PACK16:
309*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_B5G5R5A1_UNORM_PACK16:
310*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A1R5G5B5_UNORM_PACK16:
311*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R5G6B5_UNORM_PACK16:
312*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_B8G8R8A8_UNORM:
313*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_B8G8R8A8_SRGB:
314*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8G8B8A8_UNORM:
315*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8G8B8A8_SRGB:
316*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8G8_UNORM:
317*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8_UNORM:
318*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16_UNORM:
319*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16G16_UNORM:
320*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16G16B16A16_UNORM:
321*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A8B8G8R8_UNORM_PACK32:
322*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
323*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
324*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
325*03ce13f7SAndroid Build Coastguard Worker 			color[index].x = Min(Max(color[index].x, 0.0f), 1.0f);
326*03ce13f7SAndroid Build Coastguard Worker 			color[index].y = Min(Max(color[index].y, 0.0f), 1.0f);
327*03ce13f7SAndroid Build Coastguard Worker 			color[index].z = Min(Max(color[index].z, 0.0f), 1.0f);
328*03ce13f7SAndroid Build Coastguard Worker 			color[index].w = Min(Max(color[index].w, 0.0f), 1.0f);
329*03ce13f7SAndroid Build Coastguard Worker 			break;
330*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R32_SFLOAT:
331*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R32G32_SFLOAT:
332*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R32G32B32A32_SFLOAT:
333*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R32_SINT:
334*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R32G32_SINT:
335*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R32G32B32A32_SINT:
336*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R32_UINT:
337*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R32G32_UINT:
338*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R32G32B32A32_UINT:
339*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16_SFLOAT:
340*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16G16_SFLOAT:
341*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16G16B16A16_SFLOAT:
342*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
343*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16_SINT:
344*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16G16_SINT:
345*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16G16B16A16_SINT:
346*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16_UINT:
347*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16G16_UINT:
348*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R16G16B16A16_UINT:
349*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8_SINT:
350*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8G8_SINT:
351*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8G8B8A8_SINT:
352*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8_UINT:
353*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8G8_UINT:
354*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_R8G8B8A8_UINT:
355*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A8B8G8R8_UINT_PACK32:
356*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A8B8G8R8_SINT_PACK32:
357*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A2B10G10R10_UINT_PACK32:
358*03ce13f7SAndroid Build Coastguard Worker 		case VK_FORMAT_A2R10G10B10_UINT_PACK32:
359*03ce13f7SAndroid Build Coastguard Worker 			break;
360*03ce13f7SAndroid Build Coastguard Worker 		default:
361*03ce13f7SAndroid Build Coastguard Worker 			UNSUPPORTED("VkFormat: %d", int(state.colorFormat[index]));
362*03ce13f7SAndroid Build Coastguard Worker 		}
363*03ce13f7SAndroid Build Coastguard Worker 	}
364*03ce13f7SAndroid Build Coastguard Worker }
365*03ce13f7SAndroid Build Coastguard Worker 
366*03ce13f7SAndroid Build Coastguard Worker }  // namespace sw
367