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 | 895df0d | 2019-05-08 08:49:58 +0100 | [diff] [blame] | 20 | #include "Reactor/Coroutine.hpp" |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 21 | #include "Device/Context.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 | |
| 26 | namespace vk |
| 27 | { |
| 28 | class PipelineLayout; |
| 29 | } // namespace vk |
| 30 | |
| 31 | namespace sw |
| 32 | { |
| 33 | |
| 34 | using namespace rr; |
| 35 | |
| 36 | class DescriptorSetsLayout; |
Chris Forbes | 548e366 | 2019-04-25 10:00:06 -0700 | [diff] [blame] | 37 | struct Constants; |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 38 | |
| 39 | // ComputeProgram builds a SPIR-V compute shader. |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 40 | class ComputeProgram : public Coroutine<SpirvShader::YieldResult( |
Ben Clayton | 895df0d | 2019-05-08 08:49:58 +0100 | [diff] [blame] | 41 | void* data, |
| 42 | int32_t workgroupX, |
| 43 | int32_t workgroupY, |
| 44 | int32_t workgroupZ, |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 45 | void* workgroupMemory, |
Ben Clayton | 895df0d | 2019-05-08 08:49:58 +0100 | [diff] [blame] | 46 | int32_t firstSubgroup, |
| 47 | int32_t subgroupCount)> |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 48 | { |
| 49 | public: |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 50 | 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] | 51 | |
| 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 Clayton | 895df0d | 2019-05-08 08:49:58 +0100 | [diff] [blame] | 58 | void run( |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 59 | vk::DescriptorSet::Bindings const &descriptorSetBindings, |
| 60 | vk::DescriptorSet::DynamicOffsets const &descriptorDynamicOffsets, |
| 61 | PushConstantStorage const &pushConstants, |
Chris Forbes | 4a4c259 | 2019-05-13 08:53:36 -0700 | [diff] [blame] | 62 | uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ, |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 63 | uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ); |
| 64 | |
| 65 | protected: |
| 66 | void emit(); |
| 67 | |
Ben Clayton | 13dcbec | 2019-05-08 08:43:55 +0100 | [diff] [blame] | 68 | void setWorkgroupBuiltins(Int workgroupID[3]); |
| 69 | void setSubgroupBuiltins(Int workgroupID[3], SIMD::Int localInvocationIndex, Int subgroupIndex); |
Ben Clayton | c2bb50b | 2019-03-13 14:28:32 +0000 | [diff] [blame] | 70 | void setInputBuiltin(spv::BuiltIn id, std::function<void(const SpirvShader::BuiltinMapping& builtin, Array<SIMD::Float>& value)> cb); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 71 | |
| 72 | Pointer<Byte> data; // argument 0 |
| 73 | |
| 74 | struct Data |
| 75 | { |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 76 | vk::DescriptorSet::Bindings descriptorSets; |
| 77 | vk::DescriptorSet::DynamicOffsets descriptorDynamicOffsets; |
Ben Clayton | 13dcbec | 2019-05-08 08:43:55 +0100 | [diff] [blame] | 78 | 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 Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 83 | PushConstantStorage pushConstants; |
Chris Forbes | 548e366 | 2019-04-25 10:00:06 -0700 | [diff] [blame] | 84 | const Constants *constants; |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | SpirvRoutine routine; |
| 88 | SpirvShader const * const shader; |
| 89 | vk::PipelineLayout const * const pipelineLayout; |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 90 | const vk::DescriptorSet::Bindings &descriptorSets; |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | } // namespace sw |
| 94 | |
| 95 | #endif // sw_ComputeProgram_hpp |