blob: ab505928338aef52b8b57b12a3d13cc2befc6937 [file] [log] [blame]
Ben Claytonf2be26a2019-03-08 12:02:05 +00001// Copyright 2019 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#ifndef sw_ComputeProgram_hpp
16#define sw_ComputeProgram_hpp
17
18#include "SpirvShader.hpp"
19
20#include "Reactor/Reactor.hpp"
Chris Forbesa30de542019-03-18 18:51:55 -070021#include "Device/Context.hpp"
Ben Clayton225a1302019-04-02 12:28:22 +010022#include "Vulkan/VkDescriptorSet.hpp"
Ben Claytonf2be26a2019-03-08 12:02:05 +000023
24#include <functional>
25
26namespace vk
27{
28 class PipelineLayout;
29} // namespace vk
30
31namespace sw
32{
33
34 using namespace rr;
35
36 class DescriptorSetsLayout;
37
38 // ComputeProgram builds a SPIR-V compute shader.
39 class ComputeProgram : public Function<Void(Pointer<Byte>)>
40 {
41 public:
Nicolas Capens09591b82019-04-08 22:51:08 -040042 ComputeProgram(SpirvShader const *spirvShader, vk::PipelineLayout const *pipelineLayout, const vk::DescriptorSet::Bindings &descriptorSets);
Ben Claytonf2be26a2019-03-08 12:02:05 +000043
44 virtual ~ComputeProgram();
45
46 // generate builds the shader program.
47 void generate();
48
49 // run executes the compute shader routine for all workgroups.
50 // TODO(bclayton): This probably does not belong here. Consider moving.
51 static void run(
Ben Clayton225a1302019-04-02 12:28:22 +010052 Routine *routine,
53 vk::DescriptorSet::Bindings const &descriptorSetBindings,
54 vk::DescriptorSet::DynamicOffsets const &descriptorDynamicOffsets,
55 PushConstantStorage const &pushConstants,
Ben Claytonf2be26a2019-03-08 12:02:05 +000056 uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ);
57
58 protected:
59 void emit();
60
Ben Claytonc2bb50b2019-03-13 14:28:32 +000061 void setInputBuiltin(spv::BuiltIn id, std::function<void(const SpirvShader::BuiltinMapping& builtin, Array<SIMD::Float>& value)> cb);
Ben Claytonf2be26a2019-03-08 12:02:05 +000062
63 Pointer<Byte> data; // argument 0
64
65 struct Data
66 {
Ben Clayton225a1302019-04-02 12:28:22 +010067 vk::DescriptorSet::Bindings descriptorSets;
68 vk::DescriptorSet::DynamicOffsets descriptorDynamicOffsets;
Ben Claytonf2be26a2019-03-08 12:02:05 +000069 uint4 numWorkgroups;
70 uint4 workgroupID;
Chris Forbesa30de542019-03-18 18:51:55 -070071 PushConstantStorage pushConstants;
Ben Claytonf2be26a2019-03-08 12:02:05 +000072 };
73
74 SpirvRoutine routine;
75 SpirvShader const * const shader;
76 vk::PipelineLayout const * const pipelineLayout;
Nicolas Capens09591b82019-04-08 22:51:08 -040077 const vk::DescriptorSet::Bindings &descriptorSets;
Ben Claytonf2be26a2019-03-08 12:02:05 +000078 };
79
80} // namespace sw
81
82#endif // sw_ComputeProgram_hpp