Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ 9a3607b3

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
uniform int fName[NUM_FRAGMENT];        // their namess.
50
uniform vec4 fUniforms[2*NUM_FRAGMENT]; // i-th effect is 2 consecutive vec4's: [2*i], [2*i+1]. First vec4 is the Interpolated values,
51
                                        // next describes the Region, i.e. area over which the effect is active.
52
#endif    // NUM_FRAGMENT>0
53

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

    
61
void insert( vec2 ij, uint depth, uint rgba )
62
  {
63
  uint ptr = atomicCounterIncrement(u_Counter);
64

    
65
  if( ptr<u_numRecords )
66
    {
67
    ptr = 3u*ptr + u_Size.x*u_Size.y;
68

    
69
    u_Records[ptr+1u] = depth;
70
    u_Records[ptr+2u] = rgba;
71

    
72
    memoryBarrier();
73

    
74
    uint prev = uint(ij.x) + uint(ij.y) * u_Size.x;
75
    uint curr = u_Records[prev];
76

    
77
    while (true)
78
      {
79
      if ( curr==0u || depth > u_Records[curr+1u] )  // need to insert here
80
        {
81
        u_Records[ptr] = curr;     // next of new record is curr
82
        memoryBarrier();
83
        uint res = atomicCompSwap( u_Records[prev], curr, ptr );
84

    
85
        if (res==curr) break;      // done!
86
        else           curr = res; // could not insert! retry from same place in list
87
        }
88
      else                         // advance in list
89
        {
90
        prev = curr;
91
        curr = u_Records[prev];
92
        }
93
      }
94
    }
95
  }
96

    
97
//////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
uint convert(vec4 c)
100
  {
101
  return ((uint(255.0*c.r))<<24u) + ((uint(255.0*c.g))<<16u) + ((uint(255.0*c.b))<<8u) + uint(255.0*c.a);
102
  }
103

    
104
#endif   // OIT
105

    
106
//////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
void main()                    		
109
  {  
110
  vec4 color = texture(u_Texture,v_TexCoordinate);
111

    
112
#if NUM_FRAGMENT>0
113
  vec2 diff;
114
  float degree;
115
  int effect=0;
116

    
117
  for(int i=0; i<fNumEffects; i++)
118
    {
119
    diff   = (v_Position.xy - fUniforms[effect+1].xy)/fUniforms[effect+1].zw;
120
    degree = max(0.0,1.0-dot(diff,diff));
121

    
122
    // ENABLED EFFECTS WILL BE INSERTED HERE
123

    
124
    effect+=2;
125
    }
126
#endif
127

    
128
  float z = v_Normal.z;
129
  if( z<0.0 ) z = -z;
130

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

    
133
#ifdef OIT
134
  if( converted.a > 0.97 )
135
    {
136
    fragColor= converted;
137
    }
138
  else
139
    {
140
    if( converted.a > 0.0 )
141
      {
142
      const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
143
      vec2 pixel = vec2(gl_FragCoord.xy);
144
      insert(pixel, uint(S*(1.0-gl_FragCoord.z)/2.0), convert(converted) );
145
      }
146
    discard;
147
    }
148
#else
149
  fragColor = converted;
150
#endif
151
  }
(5-5/14)