1 // Copyright 2020 The Tint 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 SRC_READER_SPIRV_USAGE_H_ 16 #define SRC_READER_SPIRV_USAGE_H_ 17 18 #include <string> 19 20 namespace tint { 21 namespace reader { 22 namespace spirv { 23 24 /// Records the properties of a sampler or texture based on how it's used 25 /// by image instructions inside function bodies. 26 /// 27 /// For example: 28 /// 29 /// If %X is the "Image" parameter of an OpImageWrite instruction then 30 /// - The memory object declaration underlying %X will gain 31 /// AddStorageWriteTexture usage 32 /// 33 /// If %Y is the "Sampled Image" parameter of an OpImageSampleDrefExplicitLod 34 /// instruction, and %Y is composed from sampler %YSam and image %YIm, then: 35 /// - The memory object declaration underlying %YSam will gain 36 /// AddComparisonSampler usage 37 /// - The memory object declaration unederlying %YIm will gain 38 /// AddSampledTexture and AddDepthTexture usages 39 class Usage { 40 public: 41 /// Constructor 42 Usage(); 43 /// Copy constructor 44 /// @param other the Usage to clone 45 Usage(const Usage& other); 46 /// Destructor 47 ~Usage(); 48 49 /// @returns true if this usage is internally consistent 50 bool IsValid() const; 51 /// @returns true if the usage fully determines a WebGPU binding type. 52 bool IsComplete() const; 53 54 /// @returns true if this usage is a sampler usage. IsSampler()55 bool IsSampler() const { return is_sampler_; } 56 /// @returns true if this usage is a comparison sampler usage. IsComparisonSampler()57 bool IsComparisonSampler() const { return is_comparison_sampler_; } 58 59 /// @returns true if this usage is a texture usage. IsTexture()60 bool IsTexture() const { return is_texture_; } 61 /// @returns true if this usage is a sampled texture usage. IsSampledTexture()62 bool IsSampledTexture() const { return is_sampled_; } 63 /// @returns true if this usage is a multisampled texture usage. IsMultisampledTexture()64 bool IsMultisampledTexture() const { return is_multisampled_; } 65 /// @returns true if this usage is a dpeth texture usage. IsDepthTexture()66 bool IsDepthTexture() const { return is_depth_; } 67 /// @returns true if this usage is a read-only storage texture IsStorageReadTexture()68 bool IsStorageReadTexture() const { return is_storage_read_; } 69 /// @returns true if this usage is a write-only storage texture IsStorageWriteTexture()70 bool IsStorageWriteTexture() const { return is_storage_write_; } 71 72 /// @returns true if this is a storage texture. IsStorageTexture()73 bool IsStorageTexture() const { 74 return is_storage_read_ || is_storage_write_; 75 } 76 77 /// Emits this usage to the given stream 78 /// @param out the output stream. 79 /// @returns the modified stream. 80 std::ostream& operator<<(std::ostream& out) const; 81 82 /// Equality operator 83 /// @param other the RHS of the equality test. 84 /// @returns true if `other` is identical to `*this` 85 bool operator==(const Usage& other) const; 86 87 /// Adds the usages from another usage object. 88 /// @param other the other usage 89 void Add(const Usage& other); 90 91 /// Records usage as a sampler. 92 void AddSampler(); 93 /// Records usage as a comparison sampler. 94 void AddComparisonSampler(); 95 96 /// Records usage as a texture of some kind. 97 void AddTexture(); 98 /// Records usage as a read-only storage texture. 99 void AddStorageReadTexture(); 100 /// Records usage as a write-only storage texture. 101 void AddStorageWriteTexture(); 102 /// Records usage as a sampled texture. 103 void AddSampledTexture(); 104 /// Records usage as a multisampled texture. 105 void AddMultisampledTexture(); 106 /// Records usage as a depth texture. 107 void AddDepthTexture(); 108 109 /// @returns this usage object as a string. 110 std::string to_str() const; 111 112 private: 113 // Sampler properties. 114 bool is_sampler_ = false; 115 // A comparison sampler is always a sampler: 116 // |is_comparison_sampler_| implies |is_sampler_| 117 bool is_comparison_sampler_ = false; 118 119 // Texture properties. 120 // |is_texture_| is always implied by any of the others below. 121 bool is_texture_ = false; 122 bool is_sampled_ = false; 123 bool is_multisampled_ = false; // This implies it's sampled as well. 124 bool is_depth_ = false; 125 bool is_storage_read_ = false; 126 bool is_storage_write_ = false; 127 }; 128 129 inline std::ostream& operator<<(std::ostream& out, const Usage& u) { 130 return u.operator<<(out); 131 } 132 133 } // namespace spirv 134 } // namespace reader 135 } // namespace tint 136 137 #endif // SRC_READER_SPIRV_USAGE_H_ 138