1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
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
16 #include "tensorflow/lite/delegates/gpu/gl/kernels/resampler.h"
17
18 #include <algorithm>
19 #include <cstdint>
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25
26 #include "absl/memory/memory.h"
27 #include "tensorflow/lite/delegates/gpu/common/operations.h"
28 #include "tensorflow/lite/delegates/gpu/common/status.h"
29 #include "tensorflow/lite/delegates/gpu/common/types.h"
30
31 namespace tflite {
32 namespace gpu {
33 namespace gl {
34 namespace {
35
36 class Resampler : public NodeShader {
37 public:
GenerateCode(const GenerationContext & ctx,GeneratedCode * generated_code) const38 absl::Status GenerateCode(const GenerationContext& ctx,
39 GeneratedCode* generated_code) const final {
40 std::vector<Variable> parameters = {
41 {"src_height", static_cast<int>(ctx.input_shapes[0][1])},
42 {"src_width", static_cast<int>(ctx.input_shapes[0][2])},
43 };
44
45 std::string source = R"(
46 highp int X = int(gid.x);
47 highp int Y = int(gid.y);
48 highp int S = int(gid.z);
49 highp vec2 f_coords = ($input_data_1[X, Y, 0]$).xy;
50 highp vec2 f_coords_floor = floor(f_coords);
51 highp ivec4 st;
52 st.xy = ivec2(f_coords_floor.x, f_coords_floor.y);
53 st.zw = st.xy + ivec2(1, 1);
54 highp vec2 t = f_coords - f_coords_floor;
55 bool stx_in = st.x >= 0 && st.x < $src_width$;
56 bool stz_in = st.z >= 0 && st.z < $src_width$;
57 bool sty_in = st.y >= 0 && st.y < $src_height$;
58 bool stw_in = st.w >= 0 && st.w < $src_height$;
59 vec4 src0 = (stx_in && sty_in) ? $input_data_0[st.x, st.y, S]$ : vec4(0.0);
60 vec4 src1 = (stz_in && sty_in) ? $input_data_0[st.z, st.y, S]$ : vec4(0.0);
61 vec4 src2 = (stx_in && stw_in) ? $input_data_0[st.x, st.w, S]$ : vec4(0.0);
62 vec4 src3 = (stz_in && stw_in) ? $input_data_0[st.z, st.w, S]$ : vec4(0.0);
63 value_0 = mix(mix(src0, src1, t.x), mix(src2, src3, t.x), t.y);
64 )";
65 *generated_code = {
66 /*parameters=*/std::move(parameters),
67 /*objects=*/{},
68 /*shared_variables=*/{},
69 /*workload=*/uint3(),
70 /*workgroup=*/uint3(),
71 /*source_code=*/std::move(source),
72 /*input=*/IOStructure::ONLY_DEFINITIONS,
73 /*output=*/IOStructure::AUTO,
74 };
75 return absl::OkStatus();
76 }
77 };
78
79 } // namespace
80
NewResamplerNodeShader()81 std::unique_ptr<NodeShader> NewResamplerNodeShader() {
82 return std::make_unique<Resampler>();
83 }
84
85 } // namespace gl
86 } // namespace gpu
87 } // namespace tflite
88