Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ 3f12341d

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
  }
(5-5/14)