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_MULTIPLANAR_EXTERNAL_TEXTURE_H_ 16 #define SRC_TRANSFORM_MULTIPLANAR_EXTERNAL_TEXTURE_H_ 17 18 #include <unordered_map> 19 #include <utility> 20 21 #include "src/ast/struct_member.h" 22 #include "src/sem/binding_point.h" 23 #include "src/sem/intrinsic_type.h" 24 #include "src/transform/transform.h" 25 26 namespace tint { 27 namespace transform { 28 29 /// BindingPoint is an alias to sem::BindingPoint 30 using BindingPoint = sem::BindingPoint; 31 32 /// This struct identifies the binding groups and locations for new bindings to 33 /// use when transforming a texture_external instance. 34 struct BindingPoints { 35 /// The desired binding location of the texture_2d representing plane #1 when 36 /// a texture_external binding is expanded. 37 BindingPoint plane_1; 38 /// The desired binding location of the ExternalTextureParams uniform when a 39 /// texture_external binding is expanded. 40 BindingPoint params; 41 }; 42 43 /// Within the MultiplanarExternalTexture transform, each instance of a 44 /// texture_external binding is unpacked into two texture_2d<f32> bindings 45 /// representing two possible planes of a texture and a uniform buffer binding 46 /// representing a struct of parameters. Calls to textureLoad or 47 /// textureSampleLevel that contain a texture_external parameter will be 48 /// transformed into a newly generated version of the function, which can 49 /// perform the desired operation on a single RGBA plane or on seperate Y and UV 50 /// planes. 51 class MultiplanarExternalTexture 52 : public Castable<MultiplanarExternalTexture, Transform> { 53 public: 54 /// BindingsMap is a map where the key is the binding location of a 55 /// texture_external and the value is a struct containing the desired 56 /// locations for new bindings expanded from the texture_external instance. 57 using BindingsMap = std::unordered_map<BindingPoint, BindingPoints>; 58 59 /// NewBindingPoints is consumed by the MultiplanarExternalTexture transform. 60 /// Data holds information about location of each texture_external binding and 61 /// which binding slots it should expand into. 62 struct NewBindingPoints : public Castable<Data, transform::Data> { 63 /// Constructor 64 /// @param bm a map to the new binding slots to use. 65 explicit NewBindingPoints(BindingsMap bm); 66 67 /// Destructor 68 ~NewBindingPoints() override; 69 70 /// A map of new binding points to use. 71 const BindingsMap bindings_map; 72 }; 73 74 /// Constructor 75 MultiplanarExternalTexture(); 76 /// Destructor 77 ~MultiplanarExternalTexture() override; 78 79 protected: 80 struct State; 81 82 /// Runs the transform using the CloneContext built for transforming a 83 /// program. Run() is responsible for calling Clone() on the CloneContext. 84 /// @param ctx the CloneContext primed with the input program and 85 /// ProgramBuilder 86 /// @param inputs optional extra transform-specific input data 87 /// @param outputs optional extra transform-specific output data 88 void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs) override; 89 }; 90 91 } // namespace transform 92 } // namespace tint 93 94 #endif // SRC_TRANSFORM_MULTIPLANAR_EXTERNAL_TEXTURE_H_ 95