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