1 #include "Halide.h"
2 #include <stdio.h>
3 
4 #include "testing.h"
5 
6 using namespace Halide;
7 
8 // This test executes a simple kernel with a non-zero min value. The code is
9 // adapted from lesson_06_realizing_over_shifted_domains.cpp and scheduled for
10 // GLSL
shifted_domains()11 int shifted_domains() {
12 
13     // This test must be run with an OpenGL target.
14     const Target target = get_jit_target_from_environment().with_feature(Target::OpenGL);
15 
16     int errors = 0;
17 
18     Func gradient("gradient");
19     Var x("x"), y("y"), c("c");
20     gradient(x, y, c) = cast<float>(x + y);
21 
22     gradient.bound(c, 0, 1);
23     gradient.glsl(x, y, c);
24 
25     printf("Evaluating gradient from (0, 0) to (7, 7)\n");
26     Buffer<float> result(8, 8, 1);
27     gradient.realize(result, target);
28     result.copy_to_host();
29 
30     if (!Testing::check_result<float>(result, 5e-5f, [](int x, int y) { return float(x + y); }))
31         errors++;
32 
33     Buffer<float> shifted(5, 7, 1);
34     shifted.set_min(100, 50);
35 
36     printf("Evaluating gradient from (100, 50) to (104, 56)\n");
37 
38     gradient.realize(shifted, target);
39     shifted.copy_to_host();
40 
41     if (!Testing::check_result<float>(shifted, 5e-5f, [](int x, int y) { return float(x + y); }))
42         errors++;
43 
44     // Test with a negative min
45     shifted.set_min(-100, -50);
46 
47     printf("Evaluating gradient from (-100, -50) to (-96, -44)\n");
48 
49     gradient.realize(shifted, target);
50     shifted.copy_to_host();
51 
52     if (!Testing::check_result<float>(shifted, 5e-5f, [](int x, int y) { return float(x + y); }))
53         errors++;
54 
55     return errors;
56 }
57 
main()58 int main() {
59 
60     if (shifted_domains() != 0) {
61         return 1;
62     }
63 
64     printf("Success\n");
65     return 0;
66 }
67