blob: 8dfc0987928a8f39f320bfd3450d2d95bd62fa2f [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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 sinclairb5599d32022-04-07 16:55:14 +000024namespace tint::transform {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000025
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 Clayton41f8d2a2022-03-07 18:37:46 +000055class CombineSamplers final : public Castable<CombineSamplers, Transform> {
dan sinclair41e4d9a2022-05-01 14:40:55 +000056 public:
57 /// A pair of binding points.
58 using SamplerTexturePair = sem::SamplerTexturePair;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059
dan sinclair41e4d9a2022-05-01 14:40:55 +000060 /// A map from a sampler/texture pair to a named global.
61 using BindingMap = std::unordered_map<SamplerTexturePair, std::string>;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000062
dan sinclair41e4d9a2022-05-01 14:40:55 +000063 /// 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 Harrisondbc13af2022-02-21 15:19:07 +000085 /// Constructor
dan sinclair41e4d9a2022-05-01 14:40:55 +000086 CombineSamplers();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000087
88 /// Destructor
dan sinclair41e4d9a2022-05-01 14:40:55 +000089 ~CombineSamplers() override;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000090
dan sinclair41e4d9a2022-05-01 14:40:55 +000091 protected:
92 /// The PIMPL state for this transform
93 struct State;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000094
dan sinclair41e4d9a2022-05-01 14:40:55 +000095 /// 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 Harrisondbc13af2022-02-21 15:19:07 +0000102};
103
dan sinclairb5599d32022-04-07 16:55:14 +0000104} // namespace tint::transform
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000105
106#endif // SRC_TINT_TRANSFORM_COMBINE_SAMPLERS_H_