Project

General

Profile

« Previous | Next » 

Revision 5e331bc8

Added by Leszek Koltunski over 6 years ago

Update the 'main' shaders for OIT.

View differences:

src/main/res/raw/main_fragment_shader.glsl
19 19

  
20 20
precision highp float;
21 21

  
22
#if __VERSION__ != 100
23 22
in vec3 v_Position;                     // Interpolated position for this fragment.
24 23
in vec3 v_Normal;                       // Interpolated normal for this fragment.
25 24
in vec2 v_TexCoordinate;                // Interpolated texture coordinate per fragment.
25

  
26 26
out vec4 fragColor;                     // The output color
27
#define TEXTURE texture
28
#define FRAG_COLOR fragColor
29
#else
30
varying vec3 v_Position;                // Interpolated position for this fragment.
31
varying vec3 v_Normal;                  // Interpolated normal for this fragment.
32
varying vec2 v_TexCoordinate;           // Interpolated texture coordinate per fragment.
33
#define TEXTURE texture2D
34
#define FRAG_COLOR gl_FragColor
35
#endif
36 27

  
37 28
uniform sampler2D u_Texture;            // The input texture.
38 29

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

  
34
in vec2 v_Pixel;                        // location of the current fragment, in pixels
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

  
39 47
#if NUM_FRAGMENT>0
40 48
uniform int fNumEffects;                // total number of fragment effects
41 49
uniform int fName[NUM_FRAGMENT];        // their namess.
......
43 51
                                        // next describes the Region, i.e. area over which the effect is active.
44 52
#endif    // NUM_FRAGMENT>0
45 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
#endif   // OIT
97

  
46 98
//////////////////////////////////////////////////////////////////////////////////////////////
47 99

  
48 100
void main()                    		
49 101
  {  
50
  vec4 color = TEXTURE(u_Texture,v_TexCoordinate);
102
  vec4 color = texture(u_Texture,v_TexCoordinate);
51 103

  
52 104
#if NUM_FRAGMENT>0
53 105
  vec2 diff;
......
65 117
    }
66 118
#endif
67 119

  
68
  FRAG_COLOR = vec4(color.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, color.a);
120
#ifdef OIT
121
  if( frag.a > 0.95 )
122
    {
123
    fragColor= vec4(color.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, color.a);
124
    }
125
  else
126
    {
127
    if( frag.a > 0.0 )
128
      {
129
      const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
130
      insert(v_Pixel, uint(S*(1.0-depth)/2.0), convert(frag) );
131
      }
132
    discard;
133
    }
134
#else
135
  fragColor = vec4(color.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, color.a);
136
#endif
69 137
  }
src/main/res/raw/main_vertex_shader.glsl
19 19

  
20 20
precision highp float;
21 21

  
22
#if __VERSION__ != 100
23 22
in vec3 a_Position;                  // Per-vertex position.
24 23
in vec3 a_Normal;                    // Per-vertex normal vector.
25 24
in vec2 a_TexCoordinate;             // Per-vertex texture coordinate.
......
27 26
out vec3 v_endPosition;              // for Transform Feedback only
28 27
out vec3 v_Normal;                   //
29 28
out vec2 v_TexCoordinate;            //
30
#else
31
attribute vec3 a_Position;           // Per-vertex position.
32
attribute vec3 a_Normal;             // Per-vertex normal vector.
33
attribute vec2 a_TexCoordinate;      // Per-vertex texture coordinate.
34
varying vec3 v_Position;             //
35
varying vec3 v_endPosition;          // for Transform Feedback only
36
varying vec3 v_Normal;               //
37
varying vec2 v_TexCoordinate;        //
29

  
30
#ifdef OIT
31
out vec2 v_Pixel;                    // 2D pixel coords in window space
32
uniform uvec2 u_Size;                // size of the output surface, in pixels.
38 33
#endif
39 34

  
40 35
uniform vec3 u_objD;                 // half of object width x half of object height X half the depth;
......
165 160
#endif
166 161
   
167 162
  v_Position      = v;
163

  
164
#ifdef OIT
165
  v_Pixel         = (a_Position.xy + 0.5) * vec2(u_Size);
166
#endif
167

  
168 168
  v_endPosition   = v + (0.3*u_objD.x)*n;
169 169
  v_TexCoordinate = a_TexCoordinate;
170 170
  v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));

Also available in: Unified diff