1 // Copyright 2017 The Dawn Authors 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 DAWNNATIVE_PERSTAGE_H_ 16 #define DAWNNATIVE_PERSTAGE_H_ 17 18 #include "common/Assert.h" 19 #include "common/BitSetIterator.h" 20 #include "common/Constants.h" 21 #include "dawn_native/Error.h" 22 23 #include "dawn_native/dawn_platform.h" 24 25 #include <array> 26 27 namespace dawn_native { 28 29 enum class SingleShaderStage { Vertex, Fragment, Compute }; 30 31 absl::FormatConvertResult<absl::FormatConversionCharSet::kString> AbslFormatConvert( 32 SingleShaderStage value, 33 const absl::FormatConversionSpec& spec, 34 absl::FormatSink* s); 35 36 static_assert(static_cast<uint32_t>(SingleShaderStage::Vertex) < kNumStages, ""); 37 static_assert(static_cast<uint32_t>(SingleShaderStage::Fragment) < kNumStages, ""); 38 static_assert(static_cast<uint32_t>(SingleShaderStage::Compute) < kNumStages, ""); 39 40 static_assert(static_cast<uint32_t>(wgpu::ShaderStage::Vertex) == 41 (1 << static_cast<uint32_t>(SingleShaderStage::Vertex)), 42 ""); 43 static_assert(static_cast<uint32_t>(wgpu::ShaderStage::Fragment) == 44 (1 << static_cast<uint32_t>(SingleShaderStage::Fragment)), 45 ""); 46 static_assert(static_cast<uint32_t>(wgpu::ShaderStage::Compute) == 47 (1 << static_cast<uint32_t>(SingleShaderStage::Compute)), 48 ""); 49 50 BitSetIterator<kNumStages, SingleShaderStage> IterateStages(wgpu::ShaderStage stages); 51 wgpu::ShaderStage StageBit(SingleShaderStage stage); 52 53 static constexpr wgpu::ShaderStage kAllStages = 54 static_cast<wgpu::ShaderStage>((1 << kNumStages) - 1); 55 56 template <typename T> 57 class PerStage { 58 public: 59 PerStage() = default; PerStage(const T & initialValue)60 PerStage(const T& initialValue) { 61 mData.fill(initialValue); 62 } 63 64 T& operator[](SingleShaderStage stage) { 65 DAWN_ASSERT(static_cast<uint32_t>(stage) < kNumStages); 66 return mData[static_cast<uint32_t>(stage)]; 67 } 68 const T& operator[](SingleShaderStage stage) const { 69 DAWN_ASSERT(static_cast<uint32_t>(stage) < kNumStages); 70 return mData[static_cast<uint32_t>(stage)]; 71 } 72 73 T& operator[](wgpu::ShaderStage stageBit) { 74 uint32_t bit = static_cast<uint32_t>(stageBit); 75 DAWN_ASSERT(bit != 0 && IsPowerOfTwo(bit) && bit <= (1 << kNumStages)); 76 return mData[Log2(bit)]; 77 } 78 const T& operator[](wgpu::ShaderStage stageBit) const { 79 uint32_t bit = static_cast<uint32_t>(stageBit); 80 DAWN_ASSERT(bit != 0 && IsPowerOfTwo(bit) && bit <= (1 << kNumStages)); 81 return mData[Log2(bit)]; 82 } 83 84 private: 85 std::array<T, kNumStages> mData; 86 }; 87 88 } // namespace dawn_native 89 90 #endif // DAWNNATIVE_PERSTAGE_H_ 91