blob: 402003e309068eddb12784acb9ef479f33a659b3 [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
Ben Clayton895df0d2019-05-08 08:49:58 +010020#include "Reactor/Coroutine.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;
Chris Forbes548e3662019-04-25 10:00:06 -070037 struct Constants;
Ben Claytonf2be26a2019-03-08 12:02:05 +000038
39 // ComputeProgram builds a SPIR-V compute shader.
Ben Claytonecfeede2019-05-08 08:51:01 +010040 class ComputeProgram : public Coroutine<SpirvShader::YieldResult(
Ben Clayton895df0d2019-05-08 08:49:58 +010041 void* data,
42 int32_t workgroupX,
43 int32_t workgroupY,
44 int32_t workgroupZ,
Ben Claytonecfeede2019-05-08 08:51:01 +010045 void* workgroupMemory,
Ben Clayton895df0d2019-05-08 08:49:58 +010046 int32_t firstSubgroup,
47 int32_t subgroupCount)>
Ben Claytonf2be26a2019-03-08 12:02:05 +000048 {
49 public:
Nicolas Capens09591b82019-04-08 22:51:08 -040050 ComputeProgram(SpirvShader const *spirvShader, vk::PipelineLayout const *pipelineLayout, const vk::DescriptorSet::Bindings &descriptorSets);
Ben Claytonf2be26a2019-03-08 12:02:05 +000051
52 virtual ~ComputeProgram();
53
54 // generate builds the shader program.
55 void generate();
56
57 // run executes the compute shader routine for all workgroups.
Ben Clayton895df0d2019-05-08 08:49:58 +010058 void run(
Ben Clayton225a1302019-04-02 12:28:22 +010059 vk::DescriptorSet::Bindings const &descriptorSetBindings,
60 vk::DescriptorSet::DynamicOffsets const &descriptorDynamicOffsets,
61 PushConstantStorage const &pushConstants,
Chris Forbes4a4c2592019-05-13 08:53:36 -070062 uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ,
Ben Claytonf2be26a2019-03-08 12:02:05 +000063 uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ);
64
65 protected:
66 void emit();
67
Ben Clayton13dcbec2019-05-08 08:43:55 +010068 void setWorkgroupBuiltins(Int workgroupID[3]);
69 void setSubgroupBuiltins(Int workgroupID[3], SIMD::Int localInvocationIndex, Int subgroupIndex);
Ben Claytonc2bb50b2019-03-13 14:28:32 +000070 void setInputBuiltin(spv::BuiltIn id, std::function<void(const SpirvShader::BuiltinMapping& builtin, Array<SIMD::Float>& value)> cb);
Ben Claytonf2be26a2019-03-08 12:02:05 +000071
72 Pointer<Byte> data; // argument 0
73
74 struct Data
75 {
Ben Clayton225a1302019-04-02 12:28:22 +010076 vk::DescriptorSet::Bindings descriptorSets;
77 vk::DescriptorSet::DynamicOffsets descriptorDynamicOffsets;
Ben Clayton13dcbec2019-05-08 08:43:55 +010078 uint4 numWorkgroups; // [x, y, z, 0]
79 uint4 workgroupSize; // [x, y, z, 0]
80 uint32_t invocationsPerSubgroup; // SPIR-V: "SubgroupSize"
81 uint32_t subgroupsPerWorkgroup; // SPIR-V: "NumSubgroups"
82 uint32_t invocationsPerWorkgroup; // Total number of invocations per workgroup.
Chris Forbesa30de542019-03-18 18:51:55 -070083 PushConstantStorage pushConstants;
Chris Forbes548e3662019-04-25 10:00:06 -070084 const Constants *constants;
Ben Claytonf2be26a2019-03-08 12:02:05 +000085 };
86
87 SpirvRoutine routine;
88 SpirvShader const * const shader;
89 vk::PipelineLayout const * const pipelineLayout;
Nicolas Capens09591b82019-04-08 22:51:08 -040090 const vk::DescriptorSet::Bindings &descriptorSets;
Ben Claytonf2be26a2019-03-08 12:02:05 +000091 };
92
93} // namespace sw
94
95#endif // sw_ComputeProgram_hpp