Project

General

Profile

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

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

1 7f1735dc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 6a06a912 Leszek Koltunski
21 341151fc Leszek Koltunski
precision highp float;
22 c1a38ba3 Leszek Koltunski
precision highp int;
23 94f6d472 Leszek Koltunski
24 f2367b75 Leszek Koltunski
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 5e331bc8 Leszek Koltunski
28 f2367b75 Leszek Koltunski
out vec4 fragColor;                     // The output color
29 94f6d472 Leszek Koltunski
30
uniform sampler2D u_Texture;            // The input texture.
31 6a06a912 Leszek Koltunski
32 5e331bc8 Leszek Koltunski
#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 6a06a912 Leszek Koltunski
#if NUM_FRAGMENT>0
49 de77a6c5 Leszek Koltunski
uniform int fNumEffects;                     // total number of fragment effects
50 78ff6ea9 Leszek Koltunski
51 2b7d2abb Leszek Koltunski
uniform ivec4 fProperties[NUM_FRAGMENT];     // their properties, 4 ints:
52 de77a6c5 Leszek Koltunski
                                             // name of the effect, unused, unused, unused
53
54 2b7d2abb Leszek Koltunski
uniform vec4 fUniforms[3*NUM_FRAGMENT];      // i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
55 de77a6c5 Leszek Koltunski
                                             // The first vec4 is the Interpolated values,
56
                                             // second vec4: first float - cache, next 3: Center, the third - the Region.
57 78ff6ea9 Leszek Koltunski
58 03cb451d Leszek Koltunski
#endif    // NUM_FRAGMENT>0
59 6a06a912 Leszek Koltunski
60 5e331bc8 Leszek Koltunski
#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 c1a38ba3 Leszek Koltunski
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 5e331bc8 Leszek Koltunski
#endif   // OIT
111
112 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
113
114
void main()                    		
115
  {  
116 5e331bc8 Leszek Koltunski
  vec4 color = texture(u_Texture,v_TexCoordinate);
117 4018b3b7 Leszek Koltunski
118
#if NUM_FRAGMENT>0
119 b24e4719 Leszek Koltunski
  vec3 diff;
120 7cd24173 leszek
  float degree;
121
  int effect=0;
122 03cb451d Leszek Koltunski
123 6a06a912 Leszek Koltunski
  for(int i=0; i<fNumEffects; i++)
124
    {
125 b24e4719 Leszek Koltunski
    diff   = (v_Position - fUniforms[effect+1].yzw)/fUniforms[effect+2].xyz;
126 7cd24173 leszek
    degree = max(0.0,1.0-dot(diff,diff));
127 341c803d Leszek Koltunski
128 7cd24173 leszek
    // ENABLED EFFECTS WILL BE INSERTED HERE
129 03cb451d Leszek Koltunski
130 b24e4719 Leszek Koltunski
    effect+=3;
131 6a06a912 Leszek Koltunski
    }
132
#endif
133 ff8ad0a7 Leszek Koltunski
134 6544040f Leszek Koltunski
  float z = v_Normal.z;
135
  if( z<0.0 ) z = -z;
136
137 3f12341d Leszek Koltunski
  vec4 converted = vec4(color.rgb * (1.0 + 4.0*z) * 0.200, color.a);
138 001fb991 Leszek Koltunski
139 5e331bc8 Leszek Koltunski
#ifdef OIT
140 6544040f Leszek Koltunski
  if( converted.a > 0.97 )
141 5e331bc8 Leszek Koltunski
    {
142 001fb991 Leszek Koltunski
    fragColor= converted;
143 5e331bc8 Leszek Koltunski
    }
144
  else
145
    {
146 001fb991 Leszek Koltunski
    if( converted.a > 0.0 )
147 5e331bc8 Leszek Koltunski
      {
148
      const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
149 001fb991 Leszek Koltunski
      vec2 pixel = vec2(gl_FragCoord.xy);
150
      insert(pixel, uint(S*(1.0-gl_FragCoord.z)/2.0), convert(converted) );
151 5e331bc8 Leszek Koltunski
      }
152
    discard;
153
    }
154
#else
155 001fb991 Leszek Koltunski
  fragColor = converted;
156 5e331bc8 Leszek Koltunski
#endif
157 341c803d Leszek Koltunski
  }