Debian Codegraph @

src/shader-display.frag.glsl

raw
1 /*
2 * This file is part of Bino, a 3D video player.
3 *
4 * Copyright (C) 2022, 2023
5 * Martin Lambers <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 uniform sampler2D view0;
22 uniform sampler2D view1;
23
24 uniform float relativeWidth;
25 uniform float relativeHeight;
26 uniform float fragOffsetX;
27 uniform float fragOffsetY;
28
29 // This must be the same as OutputMode from modes.hpp:
30 const int Output_Left = 0;
31 const int Output_Right = 1;
32 const int Output_OpenGL_Stereo = 2;
33 const int Output_Alternating = 3;
34 const int Output_HDMI_Frame_Pack = 4;
35 const int Output_Left_Right = 5;
36 const int Output_Left_Right_Half = 6;
37 const int Output_Right_Left = 7;
38 const int Output_Right_Left_Half = 8;
39 const int Output_Top_Bottom = 9;
40 const int Output_Top_Bottom_Half = 10;
41 const int Output_Bottom_Top = 11;
42 const int Output_Bottom_Top_Half = 12;
43 const int Output_Even_Odd_Rows = 13;
44 const int Output_Even_Odd_Columns = 14;
45 const int Output_Checkerboard = 15;
46 const int Output_Red_Cyan_Dubois = 16;
47 const int Output_Red_Cyan_FullColor = 17;
48 const int Output_Red_Cyan_HalfColor = 18;
49 const int Output_Red_Cyan_Monochrome = 19;
50 const int Output_Green_Magenta_Dubois = 20;
51 const int Output_Green_Magenta_FullColor = 21;
52 const int Output_Green_Magenta_HalfColor = 22;
53 const int Output_Green_Magenta_Monochrome = 23;
54 const int Output_Amber_Blue_Dubois = 24;
55 const int Output_Amber_Blue_FullColor = 25;
56 const int Output_Amber_Blue_HalfColor = 26;
57 const int Output_Amber_Blue_Monochrome = 27;
58 const int Output_Red_Green_Monochrome = 28;
59 const int Output_Red_Blue_Monochrome = 29;
60 const int outputMode = $OUTPUT_MODE;
61 uniform int outputModeLeftRightView; // to distinguish betwenen Output_Left and Output_Right;
62 // we don't want both in separate shaders because
63 // Output_OpenGL_Stereo and Ouput_Alternating switch
64 // in-frame or between frames between those two.
65
66 smooth in vec2 vtexcoord;
67
68 layout(location = 0) out vec4 fcolor;
69
70
71 // linear RGB to luminance, as used by Mitsuba2 and pbrt
72 float rgb_to_lum(vec3 rgb)
73 {
74 return dot(rgb, vec3(0.212671, 0.715160, 0.072169));
75 }
76
77 // linear RGB to non-linear RGB
78 float to_nonlinear(float x)
79 {
80 const float c0 = 0.416666666667; // 1.0 / 2.4
81 return (x <= 0.0031308 ? (x * 12.92) : (1.055 * pow(x, c0) - 0.055));
82 }
83 vec3 rgb_to_nonlinear(vec3 rgb)
84 {
85 return vec3(to_nonlinear(rgb.r), to_nonlinear(rgb.g), to_nonlinear(rgb.b));
86 }
87
88 void main(void)
89 {
90 float tx = (vtexcoord.x - 0.5 * (1.0 - relativeWidth )) / relativeWidth;
91 float ty = (vtexcoord.y - 0.5 * (1.0 - relativeHeight)) / relativeHeight;
92 vec3 rgb = vec3(0.0, 0.0, 0.0);
93 if (outputMode == Output_HDMI_Frame_Pack) {
94 // HDMI frame pack has left view on top, right view at bottom, and adds 1/49 vertical blank space
95 // between the two views:
96 // - For 720p: 720 lines left view, 30 blank lines, 720 lines right view = 1470 lines; 1470/49=30
97 // - For 1080p: 1080 lines left view, 45 blank lines, 1080 lines right view = 2205 lines; 2205/49=45
98 // See the document "High-Definition Multimedia Interface Specification Version 1.4a Extraction
99 // of 3D Signaling Portion" from hdmi.org.
100 const float blankPortion = 1.0 / 49.0;
101 const float a = 0.5 + 0.5 * blankPortion;
102 const float b = 0.5 - 0.5 * blankPortion;
103 if (ty >= a) {
104 if (tx >= 0.0 && tx <= 1.0) {
105 float tty = (ty - a) / (1.0 - a);
106 rgb = texture(view0, vec2(tx, tty)).rgb;
107 }
108 } else if (ty < b) {
109 if (tx >= 0.0 && tx <= 1.0) {
110 float tty = ty / b;
111 rgb = texture(view1, vec2(tx, tty)).rgb;
112 }
113 }
114 } else if (outputMode == Output_Left || outputMode == Output_Right) {
115 if (outputModeLeftRightView == 0)
116 rgb = texture(view0, vec2(tx, ty)).rgb;
117 else
118 rgb = texture(view1, vec2(tx, ty)).rgb;
119 } else if (outputMode == Output_Left_Right || outputMode == Output_Left_Right_Half) {
120 if (tx < 0.5) {
121 if (ty >= 0.0 && ty <= 1.0)
122 rgb = texture(view0, vec2(2.0 * tx, ty)).rgb;
123 } else {
124 if (ty >= 0.0 && ty <= 1.0)
125 rgb = texture(view1, vec2(2.0 * tx - 1.0, ty)).rgb;
126 }
127 } else if (outputMode == Output_Right_Left || outputMode == Output_Right_Left_Half) {
128 if (tx < 0.5) {
129 if (ty >= 0.0 && ty <= 1.0)
130 rgb = texture(view1, vec2(2.0 * tx, ty)).rgb;
131 } else {
132 if (ty >= 0.0 && ty <= 1.0)
133 rgb = texture(view0, vec2(2.0 * tx - 1.0, ty)).rgb;
134 }
135 } else if (outputMode == Output_Top_Bottom || outputMode == Output_Top_Bottom_Half) {
136 if (ty >= 0.5) {
137 if (tx >= 0.0 && tx <= 1.0)
138 rgb = texture(view0, vec2(tx, 2.0 * ty - 1.0)).rgb;
139 } else {
140 if (tx >= 0.0 && tx <= 1.0)
141 rgb = texture(view1, vec2(tx, 2.0 * ty)).rgb;
142 }
143 } else if (outputMode == Output_Bottom_Top || outputMode == Output_Bottom_Top_Half) {
144 if (ty >= 0.5) {
145 if (tx >= 0.0 && tx <= 1.0)
146 rgb = texture(view1, vec2(tx, 2.0 * ty - 1.0)).rgb;
147 } else {
148 if (tx >= 0.0 && tx <= 1.0)
149 rgb = texture(view0, vec2(tx, 2.0 * ty)).rgb;
150 }
151 } else if (outputMode == Output_Even_Odd_Rows) {
152 float fragmentY = gl_FragCoord.y - 0.5 + fragOffsetY;
153 if (mod(fragmentY, 2.0) < 0.5) {
154 rgb = texture(view0, vec2(tx, ty)).rgb;
155 } else {
156 rgb = texture(view1, vec2(tx, ty)).rgb;
157 }
158 } else if (outputMode == Output_Even_Odd_Columns) {
159 float fragmentX = gl_FragCoord.x - 0.5 + fragOffsetX;
160 if (mod(fragmentX, 2.0) < 0.5) {
161 rgb = texture(view0, vec2(tx, ty)).rgb;
162 } else {
163 rgb = texture(view1, vec2(tx, ty)).rgb;
164 }
165 } else if (outputMode == Output_Checkerboard) {
166 float fragmentX = gl_FragCoord.x - 0.5 + fragOffsetX;
167 float fragmentY = gl_FragCoord.y - 0.5 + fragOffsetY;
168 if (abs(mod(fragmentX, 2.0) - mod(fragmentY, 2.0)) < 0.5) {
169 rgb = texture(view0, vec2(tx, ty)).rgb;
170 } else {
171 rgb = texture(view1, vec2(tx, ty)).rgb;
172 }
173 } else {
174 vec3 rgb0 = texture(view0, vec2(tx, ty)).rgb;
175 vec3 rgb1 = texture(view1, vec2(tx, ty)).rgb;
176 if (outputMode == Output_Red_Cyan_Dubois) {
177 // Source of this matrix: http://www.site.uottawa.ca/~edubois/anaglyph/LeastSquaresHowToPhotoshop.pdf
178 mat3 m0 = mat3(
179 0.437, -0.062, -0.048,
180 0.449, -0.062, -0.050,
181 0.164, -0.024, -0.017);
182 mat3 m1 = mat3(
183 -0.011, 0.377, -0.026,
184 -0.032, 0.761, -0.093,
185 -0.007, 0.009, 1.234);
186 rgb = m0 * rgb0 + m1 * rgb1;
187 } else if (outputMode == Output_Red_Cyan_FullColor) {
188 rgb = vec3(rgb0.r, rgb1.g, rgb1.b);
189 } else if (outputMode == Output_Red_Cyan_HalfColor) {
190 rgb = vec3(rgb_to_lum(rgb0), rgb1.g, rgb1.b);
191 } else if (outputMode == Output_Red_Cyan_Monochrome) {
192 rgb = vec3(rgb_to_lum(rgb0), rgb_to_lum(rgb1), rgb_to_lum(rgb1));
193 } else if (outputMode == Output_Green_Magenta_Dubois) {
194 // Source of this matrix: http://www.flickr.com/photos/e_dubois/5132528166/
195 mat3 m0 = mat3(
196 -0.062, 0.284, -0.015,
197 -0.158, 0.668, -0.027,
198 -0.039, 0.143, 0.021);
199 mat3 m1 = mat3(
200 0.529, -0.016, 0.009,
201 0.705, -0.015, 0.075,
202 0.024, -0.065, 0.937);
203 rgb = m0 * rgb0 + m1 * rgb1;
204 } else if (outputMode == Output_Green_Magenta_FullColor) {
205 rgb = vec3(rgb1.r, rgb0.g, rgb1.b);
206 } else if (outputMode == Output_Green_Magenta_HalfColor) {
207 rgb = vec3(rgb1.r, rgb_to_lum(rgb0), rgb1.b);
208 } else if (outputMode == Output_Green_Magenta_Monochrome) {
209 rgb = vec3(rgb_to_lum(rgb1), rgb_to_lum(rgb0), rgb_to_lum(rgb1));
210 } else if (outputMode == Output_Amber_Blue_Dubois) {
211 // Source of this matrix: http://www.flickr.com/photos/e_dubois/5230654930/
212 mat3 m0 = mat3(
213 1.062, -0.026, -0.038,
214 -0.205, 0.908, -0.173,
215 0.299, 0.068, 0.022);
216 mat3 m1 = mat3(
217 -0.016, 0.006, 0.094,
218 -0.123, 0.062, 0.185,
219 -0.017, -0.017, 0.911);
220 rgb = m0 * rgb0 + m1 * rgb1;
221 } else if (outputMode == Output_Amber_Blue_FullColor) {
222 rgb = vec3(rgb0.r, rgb0.g, rgb1.b);
223 } else if (outputMode == Output_Amber_Blue_HalfColor) {
224 rgb = vec3(rgb_to_lum(rgb0), rgb_to_lum(rgb0), rgb1.b);
225 } else if (outputMode == Output_Amber_Blue_Monochrome) {
226 rgb = vec3(rgb_to_lum(rgb0), rgb_to_lum(rgb0), rgb_to_lum(rgb1));
227 } else if (outputMode == Output_Red_Green_Monochrome) {
228 rgb = vec3(rgb_to_lum(rgb0), rgb_to_lum(rgb1), 0.0);
229 } else if (outputMode == Output_Red_Blue_Monochrome) {
230 rgb = vec3(rgb_to_lum(rgb0), 0.0, rgb_to_lum(rgb1));
231 }
232 }
233 fcolor = vec4(rgb_to_nonlinear(rgb), 1.0);
234 }