]> git.sesse.net Git - nageru/blob - tweaked_inputs.h
Make sure our uploaded RGBA textures can deal with being asked for linear gamma.
[nageru] / tweaked_inputs.h
1 #ifndef _TWEAKED_INPUTS_H
2 #define _TWEAKED_INPUTS_H 1
3
4 // Some tweaked variations of Movit inputs.
5
6 #include <movit/ycbcr_input.h>
7
8 namespace movit {
9 struct ImageFormat;
10 struct YCbCrFormat;
11 }  // namespace movit
12
13 class NonBouncingYCbCrInput : public movit::YCbCrInput {
14 public:
15         NonBouncingYCbCrInput(const movit::ImageFormat &image_format,
16                               const movit::YCbCrFormat &ycbcr_format,
17                               unsigned width, unsigned height,
18                               movit::YCbCrInputSplitting ycbcr_input_splitting = movit::YCBCR_INPUT_PLANAR)
19             : movit::YCbCrInput(image_format, ycbcr_format, width, height, ycbcr_input_splitting) {}
20
21         bool override_disable_bounce() const override { return true; }
22 };
23
24 // We use FlatInput with RGBA inputs a few places where we can't tell when
25 // uploading the texture whether it needs to be converted from sRGB to linear
26 // or not. (FlatInput deals with this if you give it pixels, but we give it
27 // already uploaded textures.)
28 //
29 // If we have GL_EXT_texture_sRGB_decode (very common, as far as I can tell),
30 // we can just always upload with the sRGB flag turned on, and then turn it off
31 // if not requested; that's sRGBSwitchingFlatInput. If not, we just need to
32 // turn off the functionality altogether, which is NonsRGBCapableFlatInput.
33 //
34 // If you're using NonsRGBCapableFlatInput, upload with GL_RGBA8.
35 // If using sRGBSwitchingFlatInput, upload with GL_SRGB8_ALPHA8.
36
37 class NonsRGBCapableFlatInput : public movit::FlatInput {
38 public:
39         NonsRGBCapableFlatInput(movit::ImageFormat format, movit::MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
40             : movit::FlatInput(format, pixel_format, type, width, height) {}
41
42         bool can_output_linear_gamma() const override { return false; }
43 };
44
45 class sRGBSwitchingFlatInput : public movit::FlatInput {
46 public:
47         sRGBSwitchingFlatInput(movit::ImageFormat format, movit::MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
48             : movit::FlatInput(format, pixel_format, type, width, height) {}
49
50         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) override
51         {
52                 movit::FlatInput::set_gl_state(glsl_program_num, prefix, sampler_num);
53
54                 // This flag is ignored for non-sRGB-uploaded textures, so we can set it
55                 // without checking can_output_linear_gamma().
56                 glActiveTexture(GL_TEXTURE0 + *sampler_num - 1);
57                 if (output_linear_gamma) {
58                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SRGB_DECODE_EXT, GL_DECODE_EXT);
59                 } else {
60                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
61                 }
62         }
63
64         bool set_int(const std::string &key, int value) override
65         {
66                 if (key == "output_linear_gamma") {
67                         output_linear_gamma = value;
68                 }
69                 return movit::FlatInput::set_int(key, value);
70         }
71
72 private:
73         int output_linear_gamma = false;
74 };
75
76
77 #endif  // !defined(_TWEAKED_INPUTS_H)