aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/CAPI/GL/CAPI_GL_DistortionShaders.h
blob: 03fd174534e2ab29375b90f72dd53de013c2f8cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/************************************************************************************
 
 Filename    :   CAPI_GL_Shaders.h
 Content     :   Distortion shader header for GL
 Created     :   November 11, 2013
 Authors     :   David Borel, Volga Aksoy
 
 Copyright   :   Copyright 2013 Oculus VR, Inc. All Rights reserved.
 
 Use of this software is subject to the terms of the Oculus Inc license
 agreement provided at the time of installation or download, or which
 otherwise accompanies this software in either electronic or hard copy form.
 
 ************************************************************************************/


#ifndef OVR_CAPI_GL_Shaders_h
#define OVR_CAPI_GL_Shaders_h


#include "CAPI_GL_Util.h"

namespace OVR { namespace CAPI { namespace GL {
    
    static const char glsl2Prefix[] =
    "#version 110\n"
    "#extension GL_ARB_shader_texture_lod : enable\n"
    "#define _FRAGCOLOR_DECLARATION\n"
    "#define _VS_IN attribute\n"
    "#define _VS_OUT varying\n"
    "#define _FS_IN varying\n"
    "#define _TEXTURELOD texture2DLod\n"
    "#define _FRAGCOLOR gl_FragColor\n";
    
    static const char glsl3Prefix[] =
    "#version 150\n"
    "#define _FRAGCOLOR_DECLARATION out vec4 FragColor;\n"
    "#define _VS_IN in\n"
    "#define _VS_OUT out\n"
    "#define _FS_IN in\n"
    "#define _TEXTURELOD textureLod\n"
    "#define _FRAGCOLOR FragColor\n";
    
    static const char SimpleQuad_vs[] =
    "uniform vec2 PositionOffset;\n"
    "uniform vec2 Scale;\n"
    
    "_VS_IN vec3 Position;\n"
    
	"void main()\n"
	"{\n"
	"	gl_Position = vec4(Position.xy * Scale + PositionOffset, 0.5, 1.0);\n"
	"}\n";
    
    const OVR::CAPI::GL::ShaderBase::Uniform SimpleQuad_vs_refl[] =
    {
        { "PositionOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
        { "Scale",          OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
    };
    
    static const char SimpleQuad_fs[] =
    "uniform vec4 Color;\n"
    
    "_FRAGCOLOR_DECLARATION\n"
    
	"void main()\n"
	"{\n"
	"    _FRAGCOLOR = Color;\n"
	"}\n";
    
    const OVR::CAPI::GL::ShaderBase::Uniform SimpleQuad_fs_refl[] =
    {
        { "Color", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 16 },
    };
    
    
    static const char Distortion_vs[] =
    "uniform vec2 EyeToSourceUVScale;\n"
    "uniform vec2 EyeToSourceUVOffset;\n"
    
    "_VS_IN vec2 Position;\n"
    "_VS_IN vec4 Color;\n"
    "_VS_IN vec2 TexCoord0;\n"
    
    "_VS_OUT vec4 oColor;\n"
    "_VS_OUT vec2 oTexCoord0;\n"
    
    "void main()\n"
    "{\n"
    "   gl_Position.x = Position.x;\n"
    "   gl_Position.y = Position.y;\n"
    "   gl_Position.z = 0.5;\n"
    "   gl_Position.w = 1.0;\n"
    // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
    // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
    "   oTexCoord0 = TexCoord0 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
    "   oTexCoord0.y = 1.0 - oTexCoord0.y;\n"
    "   oColor = Color;\n"              // Used for vignette fade.
    "}\n";
    
    const OVR::CAPI::GL::ShaderBase::Uniform Distortion_vs_refl[] =
    {
        { "EyeToSourceUVScale",  OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
        { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
    };
    
    static const char Distortion_fs[] =
    "uniform sampler2D Texture0;\n"
    
    "_FS_IN vec4 oColor;\n"
    "_FS_IN vec2 oTexCoord0;\n"
    
    "_FRAGCOLOR_DECLARATION\n"
    
    "void main()\n"
    "{\n"
    "   _FRAGCOLOR = _TEXTURELOD(Texture0, oTexCoord0, 0.0);\n"
    "   _FRAGCOLOR.a = 1.0;\n"
    "}\n";
    
    
    static const char DistortionTimewarp_vs[] =
    "uniform vec2 EyeToSourceUVScale;\n"
    "uniform vec2 EyeToSourceUVOffset;\n"
    "uniform mat4 EyeRotationStart;\n"
    "uniform mat4 EyeRotationEnd;\n"
    
    "_VS_IN vec2 Position;\n"
    "_VS_IN vec4 Color;\n"
    "_VS_IN vec2 TexCoord0;\n"
    
    "_FS_IN vec4 oColor;\n"
    "_FS_IN vec2 oTexCoord0;\n"
    
    "void main()\n"
    "{\n"
    "   gl_Position.x = Position.x;\n"
    "   gl_Position.y = Position.y;\n"
    "   gl_Position.z = 0.0;\n"
    "   gl_Position.w = 1.0;\n"
    
    // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
    // These are now "real world" vectors in direction (x,y,1) relative to the eye of the HMD.
    "   vec3 TanEyeAngle = vec3 ( TexCoord0.x, TexCoord0.y, 1.0 );\n"
    
    // Accurate time warp lerp vs. faster
#if 1
    // Apply the two 3x3 timewarp rotations to these vectors.
	"   vec3 TransformedStart = (EyeRotationStart * vec4(TanEyeAngle, 0)).xyz;\n"
	"   vec3 TransformedEnd   = (EyeRotationEnd * vec4(TanEyeAngle, 0)).xyz;\n"
    // And blend between them.
    "   vec3 Transformed = mix ( TransformedStart, TransformedEnd, Color.a );\n"
#else
    "   mat4 EyeRotation = mix ( EyeRotationStart, EyeRotationEnd, Color.a );\n"
    "   vec3 Transformed   = EyeRotation * TanEyeAngle;\n"
#endif
    
    // Project them back onto the Z=1 plane of the rendered images.
    "   float RecipZ = 1.0 / Transformed.z;\n"
    "   vec2 Flattened = vec2 ( Transformed.x * RecipZ, Transformed.y * RecipZ );\n"
    
    // These are now still in TanEyeAngle space.
    // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
    "   vec2 SrcCoord = Flattened * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
    "   oTexCoord0 = SrcCoord;\n"
    "   oTexCoord0.y = 1.0-oTexCoord0.y;\n"
    "   oColor = vec4(Color.r, Color.r, Color.r, Color.r);\n"              // Used for vignette fade.
    "}\n";

    
    const OVR::CAPI::GL::ShaderBase::Uniform DistortionTimewarp_vs_refl[] =
    {
        { "EyeToSourceUVScale",  OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
        { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
    };
    
    static const char DistortionChroma_vs[] =
    "uniform vec2 EyeToSourceUVScale;\n"
    "uniform vec2 EyeToSourceUVOffset;\n"
    
    "_VS_IN vec2 Position;\n"
    "_VS_IN vec4 Color;\n"
    "_VS_IN vec2 TexCoord0;\n"
    "_VS_IN vec2 TexCoord1;\n"
    "_VS_IN vec2 TexCoord2;\n"
    
    "_VS_OUT vec4 oColor;\n"
    "_VS_OUT vec2 oTexCoord0;\n"
    "_VS_OUT vec2 oTexCoord1;\n"
    "_VS_OUT vec2 oTexCoord2;\n"
    
    "void main()\n"
    "{\n"
    "   gl_Position.x = Position.x;\n"
    "   gl_Position.y = Position.y;\n"
    "   gl_Position.z = 0.5;\n"
    "   gl_Position.w = 1.0;\n"
    
    // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
    // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
    "   oTexCoord0 = TexCoord0 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
    "   oTexCoord0.y = 1.0-oTexCoord0.y;\n"
    "   oTexCoord1 = TexCoord1 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
    "   oTexCoord1.y = 1.0-oTexCoord1.y;\n"
    "   oTexCoord2 = TexCoord2 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
    "   oTexCoord2.y = 1.0-oTexCoord2.y;\n"
    
    "   oColor = Color;\n" // Used for vignette fade.
    "}\n";
    
    const OVR::CAPI::GL::ShaderBase::Uniform DistortionChroma_vs_refl[] =
    {
        { "EyeToSourceUVScale",  OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
        { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
    };
    
    static const char DistortionChroma_fs[] =
    "uniform sampler2D Texture0;\n"
    
    "_FS_IN vec4 oColor;\n"
    "_FS_IN vec2 oTexCoord0;\n"
    "_FS_IN vec2 oTexCoord1;\n"
    "_FS_IN vec2 oTexCoord2;\n"
    
    "_FRAGCOLOR_DECLARATION\n"
    
    "void main()\n"
    "{\n"
    "   float ResultR = _TEXTURELOD(Texture0, oTexCoord0, 0.0).r;\n"
    "   float ResultG = _TEXTURELOD(Texture0, oTexCoord1, 0.0).g;\n"
    "   float ResultB = _TEXTURELOD(Texture0, oTexCoord2, 0.0).b;\n"
    
    "   _FRAGCOLOR = vec4(ResultR * oColor.r, ResultG * oColor.g, ResultB * oColor.b, 1.0);\n"
    "}\n";

    
    static const char DistortionTimewarpChroma_vs[] =
    "uniform vec2 EyeToSourceUVScale;\n"
    "uniform vec2 EyeToSourceUVOffset;\n"
    "uniform mat4 EyeRotationStart;\n"
    "uniform mat4 EyeRotationEnd;\n"
    
    "_VS_IN vec2 Position;\n"
    "_VS_IN vec4 Color;\n"
    "_VS_IN vec2 TexCoord0;\n"
    "_VS_IN vec2 TexCoord1;\n"
    "_VS_IN vec2 TexCoord2;\n"
    
    "_VS_OUT vec4 oColor;\n"
    "_VS_OUT vec2 oTexCoord0;\n"
    "_VS_OUT vec2 oTexCoord1;\n"
    "_VS_OUT vec2 oTexCoord2;\n"
    
    "void main()\n"
    "{\n"
    "   gl_Position.x = Position.x;\n"
    "   gl_Position.y = Position.y;\n"
    "   gl_Position.z = 0.0;\n"
    "   gl_Position.w = 1.0;\n"
    
    // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
    // These are now "real world" vectors in direction (x,y,1) relative to the eye of the HMD.
    "   vec3 TanEyeAngleR = vec3 ( TexCoord0.x, TexCoord0.y, 1.0 );\n"
    "   vec3 TanEyeAngleG = vec3 ( TexCoord1.x, TexCoord1.y, 1.0 );\n"
    "   vec3 TanEyeAngleB = vec3 ( TexCoord2.x, TexCoord2.y, 1.0 );\n"
    
    // Accurate time warp lerp vs. faster
#if 1
    // Apply the two 3x3 timewarp rotations to these vectors.
	"   vec3 TransformedRStart = (EyeRotationStart * vec4(TanEyeAngleR, 0)).xyz;\n"
	"   vec3 TransformedGStart = (EyeRotationStart * vec4(TanEyeAngleG, 0)).xyz;\n"
	"   vec3 TransformedBStart = (EyeRotationStart * vec4(TanEyeAngleB, 0)).xyz;\n"
	"   vec3 TransformedREnd   = (EyeRotationEnd * vec4(TanEyeAngleR, 0)).xyz;\n"
	"   vec3 TransformedGEnd   = (EyeRotationEnd * vec4(TanEyeAngleG, 0)).xyz;\n"
	"   vec3 TransformedBEnd   = (EyeRotationEnd * vec4(TanEyeAngleB, 0)).xyz;\n"
    
    // And blend between them.
    "   vec3 TransformedR = mix ( TransformedRStart, TransformedREnd, Color.a );\n"
    "   vec3 TransformedG = mix ( TransformedGStart, TransformedGEnd, Color.a );\n"
    "   vec3 TransformedB = mix ( TransformedBStart, TransformedBEnd, Color.a );\n"
#else
    "   mat3 EyeRotation;\n"
    "   EyeRotation[0] = mix ( EyeRotationStart[0], EyeRotationEnd[0], Color.a ).xyz;\n"
    "   EyeRotation[1] = mix ( EyeRotationStart[1], EyeRotationEnd[1], Color.a ).xyz;\n"
    "   EyeRotation[2] = mix ( EyeRotationStart[2], EyeRotationEnd[2], Color.a ).xyz;\n"
    "   vec3 TransformedR   = EyeRotation * TanEyeAngleR;\n"
    "   vec3 TransformedG   = EyeRotation * TanEyeAngleG;\n"
    "   vec3 TransformedB   = EyeRotation * TanEyeAngleB;\n"
#endif
    
    // Project them back onto the Z=1 plane of the rendered images.
    "   float RecipZR = 1.0 / TransformedR.z;\n"
    "   float RecipZG = 1.0 / TransformedG.z;\n"
    "   float RecipZB = 1.0 / TransformedB.z;\n"
    "   vec2 FlattenedR = vec2 ( TransformedR.x * RecipZR, TransformedR.y * RecipZR );\n"
    "   vec2 FlattenedG = vec2 ( TransformedG.x * RecipZG, TransformedG.y * RecipZG );\n"
    "   vec2 FlattenedB = vec2 ( TransformedB.x * RecipZB, TransformedB.y * RecipZB );\n"
    
    // These are now still in TanEyeAngle space.
    // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
    "   vec2 SrcCoordR = FlattenedR * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
    "   vec2 SrcCoordG = FlattenedG * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
    "   vec2 SrcCoordB = FlattenedB * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
    
    "   oTexCoord0 = SrcCoordR;\n"
    "   oTexCoord0.y = 1.0-oTexCoord0.y;\n"
    "   oTexCoord1 = SrcCoordG;\n"
    "   oTexCoord1.y = 1.0-oTexCoord1.y;\n"
    "   oTexCoord2 = SrcCoordB;\n"
    "   oTexCoord2.y = 1.0-oTexCoord2.y;\n"
    
    "   oColor = vec4(Color.r, Color.r, Color.r, Color.r);\n"              // Used for vignette fade.
    "}\n";
    

    const OVR::CAPI::GL::ShaderBase::Uniform DistortionTimewarpChroma_vs_refl[] =
    {
        { "EyeToSourceUVScale",  OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
        { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
        { "EyeRotationStart",    OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 16, 64 },
        { "EyeRotationEnd",      OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 80, 64 },
    };
    
}}} // OVR::CAPI::GL

#endif // OVR_CAPI_GL_Shaders_h