xref: /aosp_15_r20/external/swiftshader/src/Pipeline/VertexProgram.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 "VertexProgram.hpp"
16*03ce13f7SAndroid Build Coastguard Worker 
17*03ce13f7SAndroid Build Coastguard Worker #include "SamplerCore.hpp"
18*03ce13f7SAndroid Build Coastguard Worker #include "Device/Renderer.hpp"
19*03ce13f7SAndroid Build Coastguard Worker #include "Device/Vertex.hpp"
20*03ce13f7SAndroid Build Coastguard Worker #include "System/Debug.hpp"
21*03ce13f7SAndroid Build Coastguard Worker #include "System/Half.hpp"
22*03ce13f7SAndroid Build Coastguard Worker #include "Vulkan/VkDevice.hpp"
23*03ce13f7SAndroid Build Coastguard Worker #include "Vulkan/VkPipelineLayout.hpp"
24*03ce13f7SAndroid Build Coastguard Worker 
25*03ce13f7SAndroid Build Coastguard Worker namespace sw {
26*03ce13f7SAndroid Build Coastguard Worker 
VertexProgram(const VertexProcessor::State & state,const vk::PipelineLayout * pipelineLayout,const SpirvShader * spirvShader,const vk::DescriptorSet::Bindings & descriptorSets)27*03ce13f7SAndroid Build Coastguard Worker VertexProgram::VertexProgram(
28*03ce13f7SAndroid Build Coastguard Worker     const VertexProcessor::State &state,
29*03ce13f7SAndroid Build Coastguard Worker     const vk::PipelineLayout *pipelineLayout,
30*03ce13f7SAndroid Build Coastguard Worker     const SpirvShader *spirvShader,
31*03ce13f7SAndroid Build Coastguard Worker     const vk::DescriptorSet::Bindings &descriptorSets)
32*03ce13f7SAndroid Build Coastguard Worker     : VertexRoutine(state, pipelineLayout, spirvShader)
33*03ce13f7SAndroid Build Coastguard Worker     , descriptorSets(descriptorSets)
34*03ce13f7SAndroid Build Coastguard Worker {
35*03ce13f7SAndroid Build Coastguard Worker 	routine.setImmutableInputBuiltins(spirvShader);
36*03ce13f7SAndroid Build Coastguard Worker 
37*03ce13f7SAndroid Build Coastguard Worker 	// TODO(b/146486064): Consider only assigning these to the SpirvRoutine iff
38*03ce13f7SAndroid Build Coastguard Worker 	// they are ever going to be read.
39*03ce13f7SAndroid Build Coastguard Worker 	routine.layer = *Pointer<Int>(data + OFFSET(DrawData, layer));
40*03ce13f7SAndroid Build Coastguard Worker 	routine.instanceID = *Pointer<Int>(data + OFFSET(DrawData, instanceID));
41*03ce13f7SAndroid Build Coastguard Worker 
42*03ce13f7SAndroid Build Coastguard Worker 	routine.setInputBuiltin(spirvShader, spv::BuiltInViewIndex, [&](const Spirv::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
43*03ce13f7SAndroid Build Coastguard Worker 		assert(builtin.SizeInComponents == 1);
44*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(routine.layer));
45*03ce13f7SAndroid Build Coastguard Worker 	});
46*03ce13f7SAndroid Build Coastguard Worker 
47*03ce13f7SAndroid Build Coastguard Worker 	routine.setInputBuiltin(spirvShader, spv::BuiltInInstanceIndex, [&](const Spirv::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
48*03ce13f7SAndroid Build Coastguard Worker 		// TODO: we could do better here; we know InstanceIndex is uniform across all lanes
49*03ce13f7SAndroid Build Coastguard Worker 		assert(builtin.SizeInComponents == 1);
50*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(routine.instanceID));
51*03ce13f7SAndroid Build Coastguard Worker 	});
52*03ce13f7SAndroid Build Coastguard Worker 
53*03ce13f7SAndroid Build Coastguard Worker 	routine.setInputBuiltin(spirvShader, spv::BuiltInSubgroupSize, [&](const Spirv::BuiltinMapping &builtin, Array<SIMD::Float> &value) {
54*03ce13f7SAndroid Build Coastguard Worker 		ASSERT(builtin.SizeInComponents == 1);
55*03ce13f7SAndroid Build Coastguard Worker 		value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(SIMD::Width));
56*03ce13f7SAndroid Build Coastguard Worker 	});
57*03ce13f7SAndroid Build Coastguard Worker 
58*03ce13f7SAndroid Build Coastguard Worker 	routine.device = device;
59*03ce13f7SAndroid Build Coastguard Worker 	routine.descriptorSets = data + OFFSET(DrawData, descriptorSets);
60*03ce13f7SAndroid Build Coastguard Worker 	routine.descriptorDynamicOffsets = data + OFFSET(DrawData, descriptorDynamicOffsets);
61*03ce13f7SAndroid Build Coastguard Worker 	routine.pushConstants = data + OFFSET(DrawData, pushConstants);
62*03ce13f7SAndroid Build Coastguard Worker 	routine.constants = device + OFFSET(vk::Device, constants);
63*03ce13f7SAndroid Build Coastguard Worker }
64*03ce13f7SAndroid Build Coastguard Worker 
~VertexProgram()65*03ce13f7SAndroid Build Coastguard Worker VertexProgram::~VertexProgram()
66*03ce13f7SAndroid Build Coastguard Worker {
67*03ce13f7SAndroid Build Coastguard Worker }
68*03ce13f7SAndroid Build Coastguard Worker 
program(Pointer<UInt> & batch,UInt & vertexCount)69*03ce13f7SAndroid Build Coastguard Worker void VertexProgram::program(Pointer<UInt> &batch, UInt &vertexCount)
70*03ce13f7SAndroid Build Coastguard Worker {
71*03ce13f7SAndroid Build Coastguard Worker 	routine.vertexIndex = *Pointer<SIMD::Int>(As<Pointer<SIMD::Int>>(batch)) +
72*03ce13f7SAndroid Build Coastguard Worker 	                      SIMD::Int(*Pointer<Int>(data + OFFSET(DrawData, baseVertex)));
73*03ce13f7SAndroid Build Coastguard Worker 
74*03ce13f7SAndroid Build Coastguard Worker 	auto it = spirvShader->inputBuiltins.find(spv::BuiltInVertexIndex);
75*03ce13f7SAndroid Build Coastguard Worker 	if(it != spirvShader->inputBuiltins.end())
76*03ce13f7SAndroid Build Coastguard Worker 	{
77*03ce13f7SAndroid Build Coastguard Worker 		assert(it->second.SizeInComponents == 1);
78*03ce13f7SAndroid Build Coastguard Worker 		routine.getVariable(it->second.Id)[it->second.FirstComponent] =
79*03ce13f7SAndroid Build Coastguard Worker 		    As<SIMD::Float>(routine.vertexIndex);
80*03ce13f7SAndroid Build Coastguard Worker 	}
81*03ce13f7SAndroid Build Coastguard Worker 
82*03ce13f7SAndroid Build Coastguard Worker 	auto activeLaneMask = SIMD::Int(0xFFFFFFFF);
83*03ce13f7SAndroid Build Coastguard Worker 	ASSERT(SIMD::Width == 4);
84*03ce13f7SAndroid Build Coastguard Worker 	SIMD::Int storesAndAtomicsMask = CmpGE(SIMD::UInt(vertexCount), SIMD::UInt(1, 2, 3, 4));
85*03ce13f7SAndroid Build Coastguard Worker 	spirvShader->emit(&routine, activeLaneMask, storesAndAtomicsMask, descriptorSets);
86*03ce13f7SAndroid Build Coastguard Worker 
87*03ce13f7SAndroid Build Coastguard Worker 	spirvShader->emitEpilog(&routine);
88*03ce13f7SAndroid Build Coastguard Worker }
89*03ce13f7SAndroid Build Coastguard Worker 
90*03ce13f7SAndroid Build Coastguard Worker }  // namespace sw
91