1 // Copyright 2021 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_TRANSFORM_DECOMPOSE_MEMORY_ACCESS_H_ 16 #define SRC_TRANSFORM_DECOMPOSE_MEMORY_ACCESS_H_ 17 18 #include <string> 19 20 #include "src/ast/internal_decoration.h" 21 #include "src/transform/transform.h" 22 23 namespace tint { 24 25 // Forward declarations 26 class CloneContext; 27 28 namespace transform { 29 30 /// DecomposeMemoryAccess is a transform used to replace storage and uniform 31 /// buffer accesses with a combination of load, store or atomic functions on 32 /// primitive types. 33 class DecomposeMemoryAccess 34 : public Castable<DecomposeMemoryAccess, Transform> { 35 public: 36 /// Intrinsic is an InternalDecoration that's used to decorate a stub function 37 /// so that the HLSL transforms this into calls to 38 /// `[RW]ByteAddressBuffer.Load[N]()` or `[RW]ByteAddressBuffer.Store[N]()`, 39 /// with a possible cast. 40 class Intrinsic : public Castable<Intrinsic, ast::InternalDecoration> { 41 public: 42 /// Intrinsic op 43 enum class Op { 44 kLoad, 45 kStore, 46 kAtomicLoad, 47 kAtomicStore, 48 kAtomicAdd, 49 kAtomicSub, 50 kAtomicMax, 51 kAtomicMin, 52 kAtomicAnd, 53 kAtomicOr, 54 kAtomicXor, 55 kAtomicExchange, 56 kAtomicCompareExchangeWeak, 57 }; 58 59 /// Intrinsic data type 60 enum class DataType { 61 kU32, 62 kF32, 63 kI32, 64 kVec2U32, 65 kVec2F32, 66 kVec2I32, 67 kVec3U32, 68 kVec3F32, 69 kVec3I32, 70 kVec4U32, 71 kVec4F32, 72 kVec4I32, 73 }; 74 75 /// Constructor 76 /// @param program_id the identifier of the program that owns this node 77 /// @param o the op of the intrinsic 78 /// @param sc the storage class of the buffer 79 /// @param ty the data type of the intrinsic 80 Intrinsic(ProgramID program_id, Op o, ast::StorageClass sc, DataType ty); 81 /// Destructor 82 ~Intrinsic() override; 83 84 /// @return a short description of the internal decoration which will be 85 /// displayed as `[[internal(<name>)]]` 86 std::string InternalName() const override; 87 88 /// Performs a deep clone of this object using the CloneContext `ctx`. 89 /// @param ctx the clone context 90 /// @return the newly cloned object 91 const Intrinsic* Clone(CloneContext* ctx) const override; 92 93 /// The op of the intrinsic 94 const Op op; 95 96 /// The storage class of the buffer this intrinsic operates on 97 ast::StorageClass const storage_class; 98 99 /// The type of the intrinsic 100 const DataType type; 101 }; 102 103 /// Constructor 104 DecomposeMemoryAccess(); 105 /// Destructor 106 ~DecomposeMemoryAccess() override; 107 108 protected: 109 /// Runs the transform using the CloneContext built for transforming a 110 /// program. Run() is responsible for calling Clone() on the CloneContext. 111 /// @param ctx the CloneContext primed with the input program and 112 /// ProgramBuilder 113 /// @param inputs optional extra transform-specific input data 114 /// @param outputs optional extra transform-specific output data 115 void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs) override; 116 117 struct State; 118 }; 119 120 } // namespace transform 121 } // namespace tint 122 123 #endif // SRC_TRANSFORM_DECOMPOSE_MEMORY_ACCESS_H_ 124