Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2022 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_TINT_TRANSFORM_COMBINE_SAMPLERS_H_ |
| 16 | #define SRC_TINT_TRANSFORM_COMBINE_SAMPLERS_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | #include <unordered_map> |
| 20 | |
| 21 | #include "src/tint/sem/sampler_texture_pair.h" |
| 22 | #include "src/tint/transform/transform.h" |
| 23 | |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 24 | namespace tint::transform { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 25 | |
| 26 | /// This transform converts all separate texture/sampler refences in a |
| 27 | /// program into combined texture/samplers. This is required for GLSL, |
| 28 | /// which does not support separate texture/samplers. |
| 29 | /// |
| 30 | /// It utilizes the texture/sampler information collected by the |
| 31 | /// Resolver and stored on each sem::Function. For each function, all |
| 32 | /// separate texture/sampler parameters in the function signature are |
| 33 | /// removed. For each unique pair, if both texture and sampler are |
| 34 | /// global variables, the function passes the corresponding combined |
| 35 | /// global stored in global_combined_texture_samplers_ at the call |
| 36 | /// site. Otherwise, either the texture or sampler must be a function |
| 37 | /// parameter. In this case, a new parameter is added to the function |
| 38 | /// signature. All separate texture/sampler parameters are removed. |
| 39 | /// |
| 40 | /// All texture builtin callsites are modified to pass the combined |
| 41 | /// texture/sampler as the first argument, and separate texture/sampler |
| 42 | /// arguments are removed. |
| 43 | /// |
| 44 | /// Note that the sampler may be null, indicating that only a texture |
| 45 | /// reference was required (e.g., textureLoad). In this case, a |
| 46 | /// placeholder global sampler is used at the AST level. This will be |
| 47 | /// combined with the original texture to give a combined global, and |
| 48 | /// the placeholder removed (ignored) by the GLSL writer. |
| 49 | /// |
| 50 | /// Note that the combined samplers are actually represented by a |
| 51 | /// Texture node at the AST level, since this contains all the |
| 52 | /// information needed to represent a combined sampler in GLSL |
| 53 | /// (dimensionality, component type, etc). The GLSL writer outputs such |
| 54 | /// (Tint) Textures as (GLSL) Samplers. |
Ben Clayton | 41f8d2a | 2022-03-07 18:37:46 +0000 | [diff] [blame] | 55 | class CombineSamplers final : public Castable<CombineSamplers, Transform> { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 56 | public: |
| 57 | /// A pair of binding points. |
| 58 | using SamplerTexturePair = sem::SamplerTexturePair; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 60 | /// A map from a sampler/texture pair to a named global. |
| 61 | using BindingMap = std::unordered_map<SamplerTexturePair, std::string>; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 62 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 63 | /// The client-provided mapping from separate texture and sampler binding |
| 64 | /// points to combined sampler binding point. |
| 65 | struct BindingInfo final : public Castable<Data, transform::Data> { |
| 66 | /// Constructor |
| 67 | /// @param map the map of all (texture, sampler) -> (combined) pairs |
| 68 | /// @param placeholder the binding point to use for placeholder samplers. |
| 69 | BindingInfo(const BindingMap& map, const sem::BindingPoint& placeholder); |
| 70 | |
| 71 | /// Copy constructor |
| 72 | /// @param other the other BindingInfo to copy |
| 73 | BindingInfo(const BindingInfo& other); |
| 74 | |
| 75 | /// Destructor |
| 76 | ~BindingInfo() override; |
| 77 | |
| 78 | /// A map of bindings from (texture, sampler) -> combined sampler. |
| 79 | BindingMap binding_map; |
| 80 | |
| 81 | /// The binding point to use for placeholder samplers. |
| 82 | sem::BindingPoint placeholder_binding_point; |
| 83 | }; |
| 84 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 85 | /// Constructor |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 86 | CombineSamplers(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 87 | |
| 88 | /// Destructor |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 89 | ~CombineSamplers() override; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 90 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 91 | protected: |
| 92 | /// The PIMPL state for this transform |
| 93 | struct State; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 94 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 95 | /// Runs the transform using the CloneContext built for transforming a |
| 96 | /// program. Run() is responsible for calling Clone() on the CloneContext. |
| 97 | /// @param ctx the CloneContext primed with the input program and |
| 98 | /// ProgramBuilder |
| 99 | /// @param inputs optional extra transform-specific input data |
| 100 | /// @param outputs optional extra transform-specific output data |
| 101 | void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs) const override; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 104 | } // namespace tint::transform |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 105 | |
| 106 | #endif // SRC_TINT_TRANSFORM_COMBINE_SAMPLERS_H_ |