]> git.sesse.net Git - movit/blob - compute_shader_test.cpp
Support other output formats than GL_RGBA16F; was easier than originally feared.
[movit] / compute_shader_test.cpp
1 #include <string>
2
3 #include <epoxy/gl.h>
4 #include <assert.h>
5
6 #include "effect.h"
7 #include "flat_input.h"
8 #include "gtest/gtest.h"
9 #include "init.h"
10 #include "resource_pool.h"
11 #include "test_util.h"
12 #include "util.h"
13
14 using namespace std;
15
16 namespace movit {
17
18 // An effect that does nothing.
19 class IdentityComputeEffect : public Effect {
20 public:
21         IdentityComputeEffect() {}
22         virtual string effect_type_id() const { return "IdentityComputeEffect"; }
23         virtual bool is_compute_shader() const { return true; }
24         string output_fragment_shader() { return read_file("identity.comp"); }
25 };
26
27 TEST(ComputeShaderTest, Identity) {
28         float data[] = {
29                 0.0f, 0.25f, 0.3f,
30                 0.75f, 1.0f, 1.0f,
31         };
32         float out_data[6];
33         EffectChainTester tester(data, 3, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
34         if (!movit_compute_shaders_supported) {
35                 fprintf(stderr, "Skipping test; no support for compile shaders.\n");
36                 return;
37         }
38         tester.get_chain()->add_effect(new IdentityComputeEffect());
39         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
40
41         expect_equal(data, out_data, 3, 2);
42 }
43
44 // Like IdentityComputeEffect, but due to the alpha handling, this will be
45 // the very last effect in the chain, which means we can't output it directly
46 // to the screen.
47 class IdentityAlphaComputeEffect : public IdentityComputeEffect {
48         AlphaHandling alpha_handling() const { return DONT_CARE_ALPHA_TYPE; }
49 };
50
51 TEST(ComputeShaderTest, LastEffectInChain) {
52         float data[] = {
53                 0.0f, 0.25f, 0.3f,
54                 0.75f, 1.0f, 1.0f,
55         };
56         float out_data[6];
57         EffectChainTester tester(data, 3, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
58         if (!movit_compute_shaders_supported) {
59                 fprintf(stderr, "Skipping test; no support for compile shaders.\n");
60                 return;
61         }
62         tester.get_chain()->add_effect(new IdentityAlphaComputeEffect());
63         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
64
65         expect_equal(data, out_data, 3, 2);
66 }
67
68 TEST(ComputeShaderTest, Render8BitTo8Bit) {
69         uint8_t data[] = {
70                 14, 200, 80,
71                 90, 100, 110,
72         };
73         uint8_t out_data[6];
74         EffectChainTester tester(nullptr, 3, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
75         if (!movit_compute_shaders_supported) {
76                 fprintf(stderr, "Skipping test; no support for compile shaders.\n");
77                 return;
78         }
79         tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, 3, 2);
80         tester.get_chain()->add_effect(new IdentityAlphaComputeEffect());
81         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
82
83         expect_equal(data, out_data, 3, 2);
84 }
85
86 }  // namespace movit