Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 1 | // 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 | |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 20 | #include "Device/Context.hpp" |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 21 | #include "Reactor/Coroutine.hpp" |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 22 | #include "Vulkan/VkDescriptorSet.hpp" |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 23 | |
| 24 | #include <functional> |
| 25 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 26 | namespace vk { |
| 27 | class PipelineLayout; |
| 28 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 29 | |
| 30 | namespace sw { |
| 31 | |
| 32 | using namespace rr; |
| 33 | |
| 34 | class DescriptorSetsLayout; |
| 35 | struct Constants; |
| 36 | |
| 37 | // ComputeProgram builds a SPIR-V compute shader. |
| 38 | class ComputeProgram : public Coroutine<SpirvShader::YieldResult( |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 39 | void *data, |
| 40 | int32_t workgroupX, |
| 41 | int32_t workgroupY, |
| 42 | int32_t workgroupZ, |
| 43 | void *workgroupMemory, |
| 44 | int32_t firstSubgroup, |
| 45 | int32_t subgroupCount)> |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 46 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 47 | public: |
| 48 | ComputeProgram(SpirvShader const *spirvShader, vk::PipelineLayout const *pipelineLayout, const vk::DescriptorSet::Bindings &descriptorSets); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 49 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 50 | virtual ~ComputeProgram(); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 51 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 52 | // generate builds the shader program. |
| 53 | void generate(); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 54 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 55 | // run executes the compute shader routine for all workgroups. |
| 56 | void run( |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 57 | vk::DescriptorSet::Array const &descriptorSetObjects, |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 58 | vk::DescriptorSet::Bindings const &descriptorSetBindings, |
| 59 | vk::DescriptorSet::DynamicOffsets const &descriptorDynamicOffsets, |
| 60 | PushConstantStorage const &pushConstants, |
| 61 | uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ, |
| 62 | uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 63 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 64 | protected: |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 65 | void emit(SpirvRoutine *routine); |
| 66 | void setWorkgroupBuiltins(Pointer<Byte> data, SpirvRoutine *routine, Int workgroupID[3]); |
| 67 | void setSubgroupBuiltins(Pointer<Byte> data, SpirvRoutine *routine, Int workgroupID[3], SIMD::Int localInvocationIndex, Int subgroupIndex); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 68 | |
| 69 | struct Data |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 70 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 71 | vk::DescriptorSet::Bindings descriptorSets; |
| 72 | vk::DescriptorSet::DynamicOffsets descriptorDynamicOffsets; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 73 | uint4 numWorkgroups; // [x, y, z, 0] |
| 74 | uint4 workgroupSize; // [x, y, z, 0] |
| 75 | uint32_t invocationsPerSubgroup; // SPIR-V: "SubgroupSize" |
| 76 | uint32_t subgroupsPerWorkgroup; // SPIR-V: "NumSubgroups" |
| 77 | uint32_t invocationsPerWorkgroup; // Total number of invocations per workgroup. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 78 | PushConstantStorage pushConstants; |
| 79 | const Constants *constants; |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 82 | SpirvShader const *const shader; |
| 83 | vk::PipelineLayout const *const pipelineLayout; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 84 | const vk::DescriptorSet::Bindings &descriptorSets; |
| 85 | }; |
| 86 | |
| 87 | } // namespace sw |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 88 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 89 | #endif // sw_ComputeProgram_hpp |