Project

General

Profile

« Previous | Next » 

Revision 53b51b53

Added by Leszek Koltunski 1 day ago

remove the shaders in resources

View differences:

src/main/res/raw/blit_depth_fragment_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22

  
23
out vec4 fragColor;           // The output color
24
in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
25

  
26
uniform sampler2D u_Texture;
27
uniform sampler2D u_DepthTexture;
28

  
29
//////////////////////////////////////////////////////////////////////////////////////////////
30
// why -0.0003 ? On some drivers (for example Qualcomm's V@331, V@415 on Adreno 308 - also on
31
// some Mali devices ) without it the depth test - when postprocessing a flat 2D object - does
32
// not pass (for unknown reasons) and we don't see the Glow halo.  See for example the 'Glowing
33
// Leaf' test app.
34
// Why exactly 0.0003? Testing of the Cube app on the SM-A315F. ( Mali G52, drivr r20p0 )
35
// 0.0003 shouldn't hurt anyway.
36

  
37
void main()                    		
38
  {
39
  gl_FragDepth = texture(u_DepthTexture,v_TexCoordinate).r -0.0003;
40
  fragColor    = texture(u_Texture     ,v_TexCoordinate);
41
  }
src/main/res/raw/blit_depth_vertex_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22

  
23
in vec2 a_Position;           // Per-vertex position.
24
out vec2 v_TexCoordinate;     //
25

  
26
uniform vec2  u_TexCorr;      // when we blit from postprocessing buffers, the buffers can be
27
                              // larger than necessary (there is just one static set being
28
                              // reused!) so we need to compensate here by adjusting the texture
29
                              // coords.
30

  
31
//////////////////////////////////////////////////////////////////////////////////////////////
32

  
33
void main()
34
  {
35
  v_TexCoordinate = (a_Position + 0.5) * u_TexCorr;
36
  gl_Position     = vec4(2.0*a_Position,0.0,1.0);
37
  }
src/main/res/raw/blit_fragment_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision lowp float;
22

  
23
out vec4 fragColor;           // The output color
24
in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
25
uniform sampler2D u_Texture;  // The input texture.
26

  
27
//////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
void main()                    		
30
  {
31
  fragColor = texture(u_Texture,v_TexCoordinate);
32
  }
src/main/res/raw/blit_vertex_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision lowp float;
22

  
23
in vec2 a_Position;           // Per-vertex position.
24
out vec2 v_TexCoordinate;     //
25
uniform float u_Depth;        // distance from the near plane to render plane, in clip coords
26

  
27
//////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
void main()
30
  {
31
  v_TexCoordinate = a_Position + 0.5;
32
  gl_Position     = vec4(2.0*a_Position,u_Depth,1.0);
33
  }
src/main/res/raw/main_fragment_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22
precision highp int;
23

  
24
in vec3 v_Position;                     // Interpolated position for this fragment.
25
in vec3 v_Normal;                       // Interpolated normal for this fragment.
26
in vec2 v_TexCoordinate;                // Interpolated texture coordinate per fragment.
27

  
28
out vec4 fragColor;                     // The output color
29

  
30
uniform sampler2D u_Texture;            // The input texture.
31

  
32
#ifdef OIT
33
//////////////////////////////////////////////////////////////////////////////////////////////
34
// per-pixel linked list. Order Independent Transparency.
35

  
36
uniform uvec2 u_Size;
37
uniform uint u_numRecords;
38

  
39
layout (binding=0, offset=0) uniform atomic_uint u_Counter;
40

  
41
layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
42
  {                                          // one for each pixel in the Output rectangle.
43
  uint u_Records[];                          //
44
  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
45
                                             // triplets of (pointer,depth,rgba).
46
#endif
47

  
48
#if NUM_FRAGMENT>0
49
uniform int fNumEffects;                     // total number of fragment effects
50

  
51
uniform ivec4 fProperties[NUM_FRAGMENT];     // their properties, 4 ints:
52
                                             // name of the effect, unused, unused, unused
53

  
54
uniform vec4 fUniforms[3*NUM_FRAGMENT];      // i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
55
                                             // The first vec4 is the Interpolated values,
56
                                             // second vec4: first float - cache, next 3: Center, the third - the Region.
57

  
58
#endif    // NUM_FRAGMENT>0
59

  
60
#ifdef OIT
61
//////////////////////////////////////////////////////////////////////////////////////////////
62
// Concurrent insert to a linked list. Tim Harris, 'pragmatic implementation of non-blocking
63
// linked-lists', 2001.
64
// This arranges fragments by decreasing 'depth', so one would think - from back to front, but
65
// in main() below the depth is mapped with S*(1-depth)/2, so it is really front to back.
66

  
67
void insert( vec2 ij, uint depth, uint rgba )
68
  {
69
  uint ptr = atomicCounterIncrement(u_Counter);
70

  
71
  if( ptr<u_numRecords )
72
    {
73
    ptr = 3u*ptr + u_Size.x*u_Size.y;
74

  
75
    u_Records[ptr+1u] = depth;
76
    u_Records[ptr+2u] = rgba;
77

  
78
    memoryBarrier();
79

  
80
    uint prev = uint(ij.x) + uint(ij.y) * u_Size.x;
81
    uint curr = u_Records[prev];
82

  
83
    while (true)
84
      {
85
      if ( curr==0u || depth > u_Records[curr+1u] )  // need to insert here
86
        {
87
        u_Records[ptr] = curr;     // next of new record is curr
88
        memoryBarrier();
89
        uint res = atomicCompSwap( u_Records[prev], curr, ptr );
90

  
91
        if (res==curr) break;      // done!
92
        else           curr = res; // could not insert! retry from same place in list
93
        }
94
      else                         // advance in list
95
        {
96
        prev = curr;
97
        curr = u_Records[prev];
98
        }
99
      }
100
    }
101
  }
102

  
103
//////////////////////////////////////////////////////////////////////////////////////////////
104

  
105
uint convert(vec4 c)
106
  {
107
  return ((uint(255.0*c.r))<<24u) + ((uint(255.0*c.g))<<16u) + ((uint(255.0*c.b))<<8u) + uint(255.0*c.a);
108
  }
109

  
110
#endif   // OIT
111

  
112
//////////////////////////////////////////////////////////////////////////////////////////////
113

  
114
void main()                    		
115
  {  
116
  vec4 color = texture(u_Texture,v_TexCoordinate);
117

  
118
#if NUM_FRAGMENT>0
119
  vec3 diff;
120
  float degree;
121
  int effect=0;
122

  
123
  for(int i=0; i<fNumEffects; i++)
124
    {
125
    diff   = (v_Position - fUniforms[effect+1].yzw)/fUniforms[effect+2].xyz;
126
    degree = max(0.0,1.0-dot(diff,diff));
127

  
128
    // ENABLED EFFECTS WILL BE INSERTED HERE
129

  
130
    effect+=3;
131
    }
132
#endif
133

  
134
  float z = v_Normal.z;
135
  if( z<0.0 ) z = -z;
136

  
137
  vec4 converted = vec4(color.rgb * (1.0 + 4.0*z) * 0.200, color.a);
138

  
139
#ifdef OIT
140
  if( converted.a > 0.97 )
141
    {
142
    fragColor= converted;
143
    }
144
  else
145
    {
146
    if( converted.a > 0.0 )
147
      {
148
      const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
149
      vec2 pixel = vec2(gl_FragCoord.xy);
150
      insert(pixel, uint(S*(1.0-gl_FragCoord.z)/2.0), convert(converted) );
151
      }
152
    discard;
153
    }
154
#else
155
  fragColor = converted;
156
#endif
157
  }
src/main/res/raw/main_vertex_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22
precision highp int;
23

  
24
in vec3 a_Position;                   // Per-vertex position.
25
in vec3 a_Normal;                     // Per-vertex normal vector.
26
in vec2 a_TexCoordinate;              // Per-vertex texture coordinate.
27
in float a_Component;                 // The component a vertex belongs to.
28
                                      // to a vertex effect. An effect will only be active on a vertex iff (a_Association & vAssociation[effect]) != 0.
29
                                      // ( see VertexEffect.retSection() )
30

  
31
out vec3 v_Position;                  // for Transform Feedback only
32
out vec3 v_endPosition;               // for Transform Feedback only
33
out vec3 v_Normal;                    //
34
out vec2 v_TexCoordinate;             //
35

  
36
uniform mat4 u_MVPMatrix;             // u_MVMatrixP * projection.
37
uniform mat4 u_MVMatrixP;             // the combined model/view matrix. (for points)
38
uniform mat4 u_MVMatrixV;             // the combined model/view matrix. (for vectors)
39
                                      // which need to work differently on points and vectors
40
uniform float u_Inflate;              // how much should we inflate (>0.0) or deflate (<0.0) the mesh.
41
uniform int u_TransformFeedback;      // are we doing the transform feedback now?
42

  
43
#ifdef COMP_CENTERS
44
layout (std140) uniform componentCenter
45
  {
46
  vec4 vComCenter[MAX_COMPON];        // centers of mesh components. 4 floats: (x,y,z,unused)
47
  };
48
#endif
49

  
50
#ifdef BUGGY_UBOS
51
layout (packed) uniform componentAssociation
52
  {
53
  ivec2 vComAssoc[MAX_COMPON];        // component 'AND' and 'EQU' Associations
54
  };
55
#else
56
layout (std140) uniform componentAssociation
57
  {
58
  ivec4 vComAssoc[MAX_COMPON];        // component 'AND' and 'EQU' Associations
59
  };
60
#endif
61

  
62
#if NUM_VERTEX>0
63
uniform int vNumEffects;              // total number of vertex effects
64

  
65
layout (std140) uniform vUniformProperties
66
  {
67
  ivec4 vProperties[NUM_VERTEX];      // their properties, 4 ints:
68
                                      // 1: name of the effect
69
                                      // 2: effect's AND association
70
                                      // 3: reserved int (probably another AND assoc in the future)
71
                                      // 4: effect's EQU association
72
  };
73

  
74
layout (std140) uniform vUniformFloats
75
  {
76
  vec4 vUniforms[3*NUM_VERTEX];       // i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
77
                                      // The first vec4 is the Interpolated values,
78
                                      // second vec4: first float - cache, next 3: Center, the third -  the Region.
79
  };
80

  
81
//////////////////////////////////////////////////////////////////////////////////////////////
82
// HELPER FUNCTIONS
83
//////////////////////////////////////////////////////////////////////////////////////////////
84
// Return degree of the point as defined by the Region. Currently only supports spherical regions.
85
//
86
// Let 'PS' be the vector from point P (the current vertex) to point S (the center of the effect).
87
// Let region.xyz be the vector from point S to point O (the center point of the region sphere)
88
// Let region.w be the radius of the region sphere.
89
// (This all should work regardless if S is inside or outside of the sphere).
90
//
91
// Then, the degree of a point with respect to a given (spherical!) Region is defined by:
92
//
93
// If P is outside the sphere, return 0.
94
// Otherwise, let X be the point where the halfline SP meets the sphere - then return |PX|/|SX|,
95
// aka the 'degree' of point P.
96
//
97
// We solve the triangle OPX.
98
// We know the lengths |PO|, |OX| and the angle OPX, because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
99
// then from the law of cosines PX^2 + PO^2 - 2*PX*PO*cos(OPX) = OX^2 so PX = -a + sqrt(a^2 + OX^2 - PO^2)
100
// where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
101
// |PX|/|PS| = -b + sqrt(b^2 + (OX^2-PO^2)/PS^2) where b=PS*PO/|PS|^2 which can be computed with only one sqrt.
102

  
103
float degree(in vec4 region, in vec3 PS)
104
  {
105
  float ps_sq = dot(PS,PS);
106

  
107
  if( ps_sq==0.0 ) return 1.0;
108

  
109
  vec3 PO = PS + region.xyz;
110
  float d = region.w*region.w-dot(PO,PO);
111

  
112
  if( d<=0.0 ) return 0.0;
113

  
114
  float b = dot(PS,PO)/ps_sq;
115

  
116
  return 1.0 / (1.0 + 1.0/(sqrt(b*b + d/ps_sq)-b));
117
  }
118

  
119
#endif  // NUM_VERTEX>0
120

  
121
//////////////////////////////////////////////////////////////////////////////////////////////
122

  
123
void main()
124
  {
125
  int component = int(a_Component);
126
  vec3 n = a_Normal;
127
#ifdef COMP_CENTERS
128
  vec3 v = a_Position + u_Inflate*(a_Position - vComCenter[component].xyz);
129
#else
130
  vec3 v = a_Position + u_Inflate*a_Position;
131
#endif
132

  
133
#ifdef POSTPROCESS
134
  if( (uint(vComAssoc[component].x) & 0x80000000u) != 0u )
135
    {
136
    v = vec3(0.0, 0.0, 0.0);
137
    }
138
#endif
139

  
140
#if NUM_VERTEX>0
141
  int effect=0;
142
  int andC = vComAssoc[component].x & 0x7fffffff;
143
  int equC = vComAssoc[component].y;
144

  
145
  for(int i=0; i<vNumEffects; i++)
146
    {
147
    if( ((andC & vProperties[i].y) != 0) || (equC == vProperties[i].w) )
148
      {
149
      // ENABLED EFFECTS WILL BE INSERTED HERE
150

  
151
      }
152
    effect+=3;
153
    }
154
#endif
155

  
156
#ifdef PREAPPLY
157
  v_Position   = v;
158
  v_endPosition= n;
159
#else
160
  if( u_TransformFeedback == 1 )
161
    {
162
    vec4 tmp1 =  u_MVMatrixP * vec4(v,1.0);
163
    vec4 tmp2 =  normalize(u_MVMatrixV * vec4(n,0.0));
164

  
165
    v_Position    = vec3(tmp1);
166
    v_endPosition = vec3(tmp1+100.0*tmp2);
167
    }
168
  else
169
    {
170
    v_Position = v;
171
    }
172
#endif
173

  
174
  v_TexCoordinate = a_TexCoordinate;
175
  v_Normal        = normalize(vec3(u_MVMatrixV*vec4(n,0.0)));
176
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
177
  }                               
src/main/res/raw/normal_fragment_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision lowp float;
22

  
23
out vec4 fragColor;
24

  
25
//////////////////////////////////////////////////////////////////////////////////////////////
26

  
27
void main()
28
  {
29
  fragColor = vec4(1.0,0.0,0.0,1.0);
30
  }
src/main/res/raw/normal_vertex_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision lowp float;
22

  
23
in vec3 a_Position;
24
uniform mat4 u_Projection;
25

  
26
//////////////////////////////////////////////////////////////////////////////////////////////
27

  
28
void main()
29
  {
30
  gl_Position = u_Projection * vec4(a_Position, 1.0);
31
  }
src/main/res/raw/oit_build_fragment_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22
precision highp int;
23

  
24
out vec4 fragColor;
25
in vec2 v_TexCoordinate;
26
in vec2 v_Pixel;              // location of the current fragment, in pixels
27

  
28
uniform sampler2D u_Texture;
29
uniform sampler2D u_DepthTexture;
30

  
31
//////////////////////////////////////////////////////////////////////////////////////////////
32
// per-pixel linked list. Order Independent Transparency.
33

  
34
uniform uvec2 u_Size;
35
uniform uint u_numRecords;
36

  
37
layout (binding=0, offset=0) uniform atomic_uint u_Counter;
38

  
39
layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
40
  {                                          // one for each pixel in the Output rectangle.
41
  uint u_Records[];                          //
42
  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
43
                                             // triplets of (pointer,depth,rgba).
44

  
45
//////////////////////////////////////////////////////////////////////////////////////////////
46
// Concurrent insert to a linked list. Tim Harris, 'pragmatic implementation of non-blocking
47
// linked-lists', 2001.
48
// This arranges fragments by decreasing 'depth', so one would think - from back to front, but
49
// in main() below the depth is mapped with S*(1-depth)/2, so it is really front to back.
50

  
51
void insert( vec2 ij, uint depth, uint rgba )
52
  {
53
  uint ptr = atomicCounterIncrement(u_Counter);
54

  
55
  if( ptr<u_numRecords )
56
    {
57
    ptr = 3u*ptr + u_Size.x*u_Size.y;
58

  
59
    u_Records[ptr+1u] = depth;
60
    u_Records[ptr+2u] = rgba;
61

  
62
    memoryBarrier();
63

  
64
    uint prev = uint(ij.x) + uint(ij.y) * u_Size.x;
65
    uint curr = u_Records[prev];
66

  
67
    while (true)
68
      {
69
      if ( curr==0u || depth > u_Records[curr+1u] )  // need to insert here
70
        {
71
        u_Records[ptr] = curr;     // next of new record is curr
72
        memoryBarrier();
73
        uint res = atomicCompSwap( u_Records[prev], curr, ptr );
74

  
75
        if (res==curr) break;      // done!
76
        else           curr = res; // could not insert! retry from same place in list
77
        }
78
      else                         // advance in list
79
        {
80
        prev = curr;
81
        curr = u_Records[prev];
82
        }
83
      }
84
    }
85
  }
86

  
87
//////////////////////////////////////////////////////////////////////////////////////////////
88

  
89
uint convert(vec4 c)
90
  {
91
  return ((uint(255.0*c.r))<<24u) + ((uint(255.0*c.g))<<16u) + ((uint(255.0*c.b))<<8u) + uint(255.0*c.a);
92
  }
93

  
94
//////////////////////////////////////////////////////////////////////////////////////////////
95
// Pass2 of the OIT algorithm - build the LinkedList phase.
96

  
97
void main()                    		
98
  {
99
  vec4  color= texture(u_Texture     , v_TexCoordinate);
100
  float depth= texture(u_DepthTexture, v_TexCoordinate).r;
101

  
102
  if( color.a > 0.97 )
103
    {
104
    gl_FragDepth = depth;
105
    fragColor    = color;
106
    }
107
  else
108
    {
109
    if( color.a > 0.0 )
110
      {
111
      const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
112
      insert(v_Pixel, uint(S*(1.0-depth)/2.0), convert(color) );
113
      }
114
    discard;
115
    }
116
  }
src/main/res/raw/oit_clear_fragment_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22
precision highp int;
23

  
24
in vec2 v_TexCoordinate;
25
in vec2 v_Pixel;              // location of the current fragment, in pixels
26

  
27
//////////////////////////////////////////////////////////////////////////////////////////////
28
// per-pixel linked list. Order Independent Transparency.
29

  
30
uniform uvec2 u_Size;
31

  
32
layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
33
  {                                          // one for each pixel in the Output rectangle.
34
  uint u_Records[];                          //
35
  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
36
                                             // triplets of (pointer,depth,rgba).
37

  
38
//////////////////////////////////////////////////////////////////////////////////////////////
39
// Pass1 of the OIT algorithm - 'clear the head pointers' phase.
40
// No we cannot optimize this out by moving the 'u_Records[index]=0u' to the end of the Pass3,
41
// because between passes the size of the surface we render to might change.
42

  
43
void main()                    		
44
  {
45
  uint index= uint(v_Pixel.x) + uint(v_Pixel.y) * u_Size.x;
46
  u_Records[index] = 0u;
47
  discard;
48
  }
src/main/res/raw/oit_collapse_fragment_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22
precision highp int;
23

  
24
out vec4 fragColor;           // The output color
25
in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
26
in vec2 v_Pixel;              // location of the current fragment, in pixels
27

  
28
uniform sampler2D u_DepthTexture;
29

  
30
//////////////////////////////////////////////////////////////////////////////////////////////
31
// per-pixel linked list. Order Independent Transparency.
32

  
33
uniform uvec2 u_Size;
34

  
35
layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
36
  {                                          // one for each pixel in the Output rectangle.
37
  uint u_Records[];                          //
38
  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
39
                                             // triplets of (pointer,depth,rgba).
40

  
41
//////////////////////////////////////////////////////////////////////////////////////////////
42
// Pass3 of the OIT algorithm - traverse the per-pixel LinkedList and cut occluded fragments.
43

  
44
void main()                    		
45
  {
46
  uint prev = uint(v_Pixel.x) + uint(v_Pixel.y) * u_Size.x;
47
  uint curr = u_Records[prev];
48

  
49
  if (curr != 0u)
50
    {
51
    const float S= 2147483647.0;
52
    float depth = texture(u_DepthTexture, v_TexCoordinate).r;
53
    uint texdepth = uint(S*(1.0-depth)/2.0);
54

  
55
    while( curr != 0u )
56
      {
57
      if( u_Records[curr+1u] <= texdepth )
58
        {
59
        u_Records[prev] = 0u;
60
        break;
61
        }
62

  
63
      prev = curr;
64
      curr = u_Records[curr];
65
      }
66
    }
67
  else discard;
68
  }
src/main/res/raw/oit_render_fragment_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22
precision highp int;
23

  
24
out vec4 fragColor;           // The output color
25
in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
26
in vec2 v_Pixel;              // location of the current fragment, in pixels
27

  
28
//////////////////////////////////////////////////////////////////////////////////////////////
29
// per-pixel linked list. Order Independent Transparency.
30

  
31
uniform uvec2 u_Size;
32

  
33
layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
34
  {                                          // one for each pixel in the Output rectangle.
35
  uint u_Records[];                          //
36
  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
37
                                             // triplets of (pointer,depth,rgba).
38

  
39
//////////////////////////////////////////////////////////////////////////////////////////////
40

  
41
vec4 convert(uint rgba)
42
  {
43
  return vec4( float((rgba>>24u)&255u),float((rgba>>16u)&255u),float((rgba>>8u)&255u),float(rgba&255u) ) / 255.0;
44
  }
45

  
46
//////////////////////////////////////////////////////////////////////////////////////////////
47
// A over B (https://en.wikipedia.org/wiki/Alpha_compositing)
48

  
49
vec4 blend(vec4 A,vec4 B)
50
  {
51
  float b = B.a * (1.0-A.a);
52
  float a = A.a + b;
53

  
54
  return vec4( (A.rgb*A.a + B.rgb*b)/a , a );
55
  }
56

  
57
//////////////////////////////////////////////////////////////////////////////////////////////
58
// Pass4 of the OIT algorithm - keep traversing the linked list, build the final color and blend it.
59

  
60
void main()                    		
61
  {
62
  uint prev = uint(v_Pixel.x) + uint(v_Pixel.y) * u_Size.x;
63
  uint curr = u_Records[prev];
64

  
65
  if (curr != 0u)
66
    {
67
    const float S= 2147483647.0;
68
    gl_FragDepth = 1.0 - 2.0*float(u_Records[curr+1u])/S;
69
    vec4 color   = convert(u_Records[curr+2u]);
70
    curr = u_Records[curr];
71

  
72
    while (curr != 0u)
73
      {
74
      color = blend( color , convert(u_Records[curr+2u]) );
75
      curr = u_Records[curr];
76
      }
77

  
78
    fragColor = color;
79
    }
80
  else discard;
81
  }
src/main/res/raw/oit_vertex_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22
precision highp int;
23

  
24
in vec2 a_Position;           // Per-vertex position.
25
out vec2 v_TexCoordinate;     //
26
out vec2 v_Pixel;             // 2D pixel coords in window space
27

  
28
uniform float u_Depth;        // distance from the near plane to render plane, in clip coords
29
uniform vec2  u_TexCorr;      // when we blit from postprocessing buffers, the buffers can be
30
                              // larger than necessary (there is just one static set being
31
                              // reused!) so we need to compensate here by adjusting the texture
32
                              // coords.
33

  
34
uniform uvec2 u_Size;         // size of the output surface, in pixels.
35

  
36
//////////////////////////////////////////////////////////////////////////////////////////////
37

  
38
void main()
39
  {
40
  v_TexCoordinate = (a_Position + 0.5) * u_TexCorr;
41
  v_Pixel         = (a_Position + 0.5) * vec2(u_Size);
42
  gl_Position     = vec4(2.0*a_Position,u_Depth,1.0);
43
  }
src/main/res/raw/preprocess_fragment_shader.glsl
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
precision highp float;
22

  
23
in vec2 v_TexCoordinate;                // Interpolated texture coordinate per fragment.
24
out vec4 fragColor;
25
uniform vec4 u_Color;
26
uniform sampler2D u_Texture;            // The input texture.
27

  
28
//////////////////////////////////////////////////////////////////////////////////////////////
29

  
30
void main()
31
  {
32
  vec4 color = texture(u_Texture,v_TexCoordinate);
33
  fragColor  = vec4(u_Color.rgb, sign(color.a)*u_Color.a);
34
  }

Also available in: Unified diff