Fix an issue where a (cached) shader program could be used from multiple
[movit] / test_util.cpp
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <algorithm>
5 #include <epoxy/gl.h>
6 #include <gtest/gtest.h>
7 #include <gtest/gtest-message.h>
8
9 #include "flat_input.h"
10 #include "init.h"
11 #include "resource_pool.h"
12 #include "test_util.h"
13 #include "util.h"
14
15 using namespace std;
16
17 namespace movit {
18
19 class Input;
20
21 namespace {
22
23 // Not thread-safe, but this isn't a big problem for testing.
24 ResourcePool *get_static_pool()
25 {
26         static ResourcePool *resource_pool = NULL;
27         if (!resource_pool) {
28                 resource_pool = new ResourcePool();
29         }
30         return resource_pool;
31 }
32
33 // Flip upside-down to compensate for different origin.
34 template<class T>
35 void vertical_flip(T *data, unsigned width, unsigned height)
36 {
37         for (unsigned y = 0; y < height / 2; ++y) {
38                 unsigned flip_y = height - y - 1;
39                 for (unsigned x = 0; x < width; ++x) {
40                         swap(data[y * width + x], data[flip_y * width + x]);
41                 }
42         }
43 }
44
45 }  // namespace
46
47 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
48                                      MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
49                                      GLenum framebuffer_format)
50         : chain(width, height, get_static_pool()),
51           width(width),
52           height(height),
53           framebuffer_format(framebuffer_format),
54           output_added(false),
55           finalized(false)
56 {
57         CHECK(init_movit(".", MOVIT_DEBUG_OFF));
58
59         if (data != NULL) {
60                 add_input(data, pixel_format, color_space, gamma_curve);
61         }
62 }
63
64 EffectChainTester::~EffectChainTester()
65 {
66 }
67
68 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
69 {
70         ImageFormat format;
71         format.color_space = color_space;
72         format.gamma_curve = gamma_curve;
73
74         if (input_width == -1) {
75                 input_width = width;
76         }
77         if (input_height == -1) {
78                 input_height = height;
79         }
80
81         FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, input_width, input_height);
82         input->set_pixel_data(data);
83         chain.add_input(input);
84         return input;
85 }
86
87 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
88 {
89         ImageFormat format;
90         format.color_space = color_space;
91         format.gamma_curve = gamma_curve;
92
93         if (input_width == -1) {
94                 input_width = width;
95         }
96         if (input_height == -1) {
97                 input_height = height;
98         }
99
100         FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, input_width, input_height);
101         input->set_pixel_data(data);
102         chain.add_input(input);
103         return input;
104 }
105
106 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
107 {
108         internal_run<float>(out_data, NULL, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
109 }
110
111 void EffectChainTester::run(float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
112 {
113         internal_run<float>(out_data, out_data2, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
114 }
115
116 void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
117 {
118         internal_run(out_data, out_data2, out_data3, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
119 }
120
121 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
122 {
123         internal_run<unsigned char>(out_data, NULL, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
124 }
125
126 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
127 {
128         internal_run<unsigned char>(out_data, out_data2, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
129 }
130
131 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, unsigned char *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
132 {
133         internal_run(out_data, out_data2, out_data3, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
134 }
135
136 template<class T>
137 void EffectChainTester::internal_run(T *out_data, T *out_data2, T *out_data3, GLenum internal_format, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
138 {
139         if (!finalized) {
140                 finalize_chain(color_space, gamma_curve, alpha_format);
141         }
142
143         GLuint type;
144         if (framebuffer_format == GL_RGBA8) {
145                 type = GL_UNSIGNED_BYTE;
146         } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
147                 type = GL_FLOAT;
148         } else {
149                 // Add more here as needed.
150                 assert(false);
151         }
152
153         unsigned num_outputs;
154         if (out_data3 != NULL) {
155                 num_outputs = 3;
156         } else if (out_data2 != NULL) {
157                 num_outputs = 2;
158         } else {
159                 num_outputs = 1;
160         }
161
162         GLuint fbo, texnum[3];
163
164         glGenTextures(num_outputs, texnum);
165         check_error();
166         for (unsigned i = 0; i < num_outputs; ++i) {
167                 glBindTexture(GL_TEXTURE_2D, texnum[i]);
168                 check_error();
169                 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, NULL);
170                 check_error();
171         }
172
173         glGenFramebuffers(1, &fbo);
174         check_error();
175         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
176         check_error();
177         for (unsigned i = 0; i < num_outputs; ++i) {
178                 glFramebufferTexture2D(
179                         GL_FRAMEBUFFER,
180                         GL_COLOR_ATTACHMENT0 + i,
181                         GL_TEXTURE_2D,
182                         texnum[i],
183                         0);
184                 check_error();
185         }
186
187         GLenum bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
188         glDrawBuffers(num_outputs, bufs);
189
190         chain.render_to_fbo(fbo, width, height);
191
192         T *data[3] = { out_data, out_data2, out_data3 };
193
194         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
195         check_error();
196         for (unsigned i = 0; i < num_outputs; ++i) {
197                 T *ptr = data[i];
198                 glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
199                 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
200                         // GLES will only read GL_RGBA.
201                         T *temp = new T[width * height * 4];
202                         glReadPixels(0, 0, width, height, GL_RGBA, internal_format, temp);
203                         check_error();
204                         if (format == GL_ALPHA) {
205                                 for (unsigned i = 0; i < width * height; ++i) {
206                                         ptr[i] = temp[i * 4 + 3];
207                                 }
208                         } else if (format == GL_BLUE) {
209                                 for (unsigned i = 0; i < width * height; ++i) {
210                                         ptr[i] = temp[i * 4 + 2];
211                                 }
212                         } else {
213                                 for (unsigned i = 0; i < width * height; ++i) {
214                                         ptr[i] = temp[i * 4];
215                                 }
216                         }
217                         delete[] temp;
218                 } else {
219                         glReadPixels(0, 0, width, height, format, internal_format, ptr);
220                         check_error();
221                 }
222
223                 if (format == GL_RGBA) {
224                         vertical_flip(ptr, width * 4, height);
225                 } else {
226                         vertical_flip(ptr, width, height);
227                 }
228         }
229
230         glDeleteFramebuffers(1, &fbo);
231         check_error();
232         glDeleteTextures(num_outputs, texnum);
233         check_error();
234 }
235
236 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
237 {
238         chain.add_output(format, alpha_format);
239         output_added = true;
240 }
241
242 void EffectChainTester::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format, const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting)
243 {
244         chain.add_ycbcr_output(format, alpha_format, ycbcr_format, output_splitting);
245         output_added = true;
246 }
247
248 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
249 {
250         assert(!finalized);
251         if (!output_added) {
252                 ImageFormat image_format;
253                 image_format.color_space = color_space;
254                 image_format.gamma_curve = gamma_curve;
255                 chain.add_output(image_format, alpha_format);
256                 output_added = true;
257         }
258         chain.finalize();
259         finalized = true;
260 }
261
262 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
263 {
264         float largest_difference = -1.0f;
265         float squared_difference = 0.0f;
266         int largest_diff_x = -1, largest_diff_y = -1;
267
268         for (unsigned y = 0; y < height; ++y) {
269                 for (unsigned x = 0; x < width; ++x) {
270                         float diff = ref[y * width + x] - result[y * width + x];
271                         if (fabs(diff) > largest_difference) {
272                                 largest_difference = fabs(diff);
273                                 largest_diff_x = x;
274                                 largest_diff_y = y;
275                         }
276                         squared_difference += diff * diff;
277                 }
278         }
279
280         EXPECT_LT(largest_difference, largest_difference_limit)
281                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
282                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
283                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
284
285         float rms = sqrt(squared_difference) / (width * height);
286         EXPECT_LT(rms, rms_limit);
287
288         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
289                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
290
291                 fprintf(stderr, "Reference:\n");
292                 for (unsigned y = 0; y < height; ++y) {
293                         for (unsigned x = 0; x < width; ++x) {
294                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
295                         }
296                         fprintf(stderr, "\n");
297                 }
298
299                 fprintf(stderr, "\nResult:\n");
300                 for (unsigned y = 0; y < height; ++y) {
301                         for (unsigned x = 0; x < width; ++x) {
302                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
303                         }
304                         fprintf(stderr, "\n");
305                 }
306         }
307 }
308
309 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
310 {
311         assert(width > 0);
312         assert(height > 0);
313
314         float *ref_float = new float[width * height];
315         float *result_float = new float[width * height];
316
317         for (unsigned y = 0; y < height; ++y) {
318                 for (unsigned x = 0; x < width; ++x) {
319                         ref_float[y * width + x] = ref[y * width + x];
320                         result_float[y * width + x] = result[y * width + x];
321                 }
322         }
323
324         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
325
326         delete[] ref_float;
327         delete[] result_float;
328 }
329
330 void test_accuracy(const float *expected, const float *result, unsigned num_values, double absolute_error_limit, double relative_error_limit, double local_relative_error_limit, double rms_limit)
331 {
332         double squared_difference = 0.0;
333         for (unsigned i = 0; i < num_values; ++i) {
334                 double absolute_error = fabs(expected[i] - result[i]);
335                 squared_difference += absolute_error * absolute_error;
336                 EXPECT_LT(absolute_error, absolute_error_limit);
337
338                 if (expected[i] > 0.0) {
339                         double relative_error = fabs(absolute_error / expected[i]);
340
341                         EXPECT_LT(relative_error, relative_error_limit);
342                 }
343                 if (i < num_values - 1) {
344                         double delta = expected[i + 1] - expected[i];
345                         double local_relative_error = fabs(absolute_error / delta);
346                         EXPECT_LT(local_relative_error, local_relative_error_limit);
347                 }
348         }
349         double rms = sqrt(squared_difference) / num_values;
350         EXPECT_LT(rms, rms_limit);
351 }
352
353 double srgb_to_linear(double x)
354 {
355         // From the Wikipedia article on sRGB.
356         if (x < 0.04045) {
357                 return x / 12.92;
358         } else {
359                 return pow((x + 0.055) / 1.055, 2.4);
360         }
361 }
362
363 double linear_to_srgb(double x)
364 {
365         // From the Wikipedia article on sRGB.
366         if (x < 0.0031308) {
367                 return 12.92 * x;
368         } else {
369                 return 1.055 * pow(x, 1.0 / 2.4) - 0.055;
370         }
371 }
372
373 }  // namespace movit