Project

General

Profile

Download (5.99 KB) Statistics
| Branch: | Revision:

library / src / main / res / raw / main_fragment_shader.glsl @ de77a6c5

1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4
// This file is part of Distorted.                                                          //
5
//                                                                                          //
6
// Distorted is free software: you can redistribute it and/or modify                        //
7
// it under the terms of the GNU General Public License as published by                     //
8
// the Free Software Foundation, either version 2 of the License, or                        //
9
// (at your option) any later version.                                                      //
10
//                                                                                          //
11
// Distorted 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                            //
14
// GNU General Public License for more details.                                             //
15
//                                                                                          //
16
// You should have received a copy of the GNU General Public License                        //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18
//////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
precision highp float;
21
precision highp int;
22

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

    
27
out vec4 fragColor;                     // The output color
28

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

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

    
35
uniform uvec2 u_Size;
36
uniform uint u_numRecords;
37

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

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

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

    
50
layout (std140) uniform fUniformProperties
51
  {
52
  ivec4 fProperties[NUM_FRAGMENT];           // their properties, 4 ints:
53
                                             // name of the effect, unused, unused, unused
54
  };
55

    
56
layout (std140) uniform fUniformFloats
57
  {
58
  vec4 fUniforms[3*NUM_FRAGMENT];            // i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
59
                                             // The first vec4 is the Interpolated values,
60
                                             // second vec4: first float - cache, next 3: Center, the third - the Region.
61
  };
62

    
63
#endif    // NUM_FRAGMENT>0
64

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

    
72
void insert( vec2 ij, uint depth, uint rgba )
73
  {
74
  uint ptr = atomicCounterIncrement(u_Counter);
75

    
76
  if( ptr<u_numRecords )
77
    {
78
    ptr = 3u*ptr + u_Size.x*u_Size.y;
79

    
80
    u_Records[ptr+1u] = depth;
81
    u_Records[ptr+2u] = rgba;
82

    
83
    memoryBarrier();
84

    
85
    uint prev = uint(ij.x) + uint(ij.y) * u_Size.x;
86
    uint curr = u_Records[prev];
87

    
88
    while (true)
89
      {
90
      if ( curr==0u || depth > u_Records[curr+1u] )  // need to insert here
91
        {
92
        u_Records[ptr] = curr;     // next of new record is curr
93
        memoryBarrier();
94
        uint res = atomicCompSwap( u_Records[prev], curr, ptr );
95

    
96
        if (res==curr) break;      // done!
97
        else           curr = res; // could not insert! retry from same place in list
98
        }
99
      else                         // advance in list
100
        {
101
        prev = curr;
102
        curr = u_Records[prev];
103
        }
104
      }
105
    }
106
  }
107

    
108
//////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
uint convert(vec4 c)
111
  {
112
  return ((uint(255.0*c.r))<<24u) + ((uint(255.0*c.g))<<16u) + ((uint(255.0*c.b))<<8u) + uint(255.0*c.a);
113
  }
114

    
115
#endif   // OIT
116

    
117
//////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
void main()                    		
120
  {  
121
  vec4 color = texture(u_Texture,v_TexCoordinate);
122

    
123
#if NUM_FRAGMENT>0
124
  vec3 diff;
125
  float degree;
126
  int effect=0;
127

    
128
  for(int i=0; i<fNumEffects; i++)
129
    {
130
    diff   = (v_Position - fUniforms[effect+1].yzw)/fUniforms[effect+2].xyz;
131
    degree = max(0.0,1.0-dot(diff,diff));
132

    
133
    // ENABLED EFFECTS WILL BE INSERTED HERE
134

    
135
    effect+=3;
136
    }
137
#endif
138

    
139
  float z = v_Normal.z;
140
  if( z<0.0 ) z = -z;
141

    
142
  vec4 converted = vec4(color.rgb * (1.0 + 7.0*z) * 0.125, color.a);
143

    
144
#ifdef OIT
145
  if( converted.a > 0.97 )
146
    {
147
    fragColor= converted;
148
    }
149
  else
150
    {
151
    if( converted.a > 0.0 )
152
      {
153
      const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
154
      vec2 pixel = vec2(gl_FragCoord.xy);
155
      insert(pixel, uint(S*(1.0-gl_FragCoord.z)/2.0), convert(converted) );
156
      }
157
    discard;
158
    }
159
#else
160
  fragColor = converted;
161
#endif
162
  }
(5-5/14)