Project

General

Profile

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

library / src / main / res / raw / oit_render_fragment_shader.glsl @ b52f9415

1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 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
out vec4 fragColor;           // The output color
24
in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
25
in vec2 v_Pixel;              // location of the current fragment, in pixels
26

    
27
uniform sampler2D u_Texture;
28
uniform sampler2D u_DepthTexture;
29

    
30
//////////////////////////////////////////////////////////////////////////////////////////////
31
// per-pixel linked list. Order Independent Transparency.
32

    
33
uniform vec2 u_Size;
34

    
35
layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
36
  {                                          // one for each pixel in the Output rectangle.
37
  uint u_Records[];                          //
38
  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
39
                                             // triplets of (pointer,depth,rgba).
40

    
41
//////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
vec4 convert(uint rgba)
44
  {
45
  return vec4( float((rgba>>24u)&255u),float((rgba>>16u)&255u),float((rgba>>8u)&255u),float(rgba&255u) ) / 255.0;
46
  }
47

    
48
//////////////////////////////////////////////////////////////////////////////////////////////
49
// https://en.wikipedia.org/wiki/Alpha_compositing (premultiplied)
50

    
51
vec4 blend(vec4 clr,vec4 srf)
52
  {
53
  return clr + (1.0 - clr.a) * vec4(srf.rgb * srf.a , srf.a);
54
  }
55

    
56
//////////////////////////////////////////////////////////////////////////////////////////////
57
// Pass3 of the OIT algorithm - traverse the per-pixel LinkedList and build the final color.
58

    
59
void main()                    		
60
  {
61
  float texdepth = texture(u_DepthTexture, v_TexCoordinate).r;
62
  vec4  color    = texture(u_Texture     , v_TexCoordinate);
63
  uint  index    = uint(v_Pixel.x + v_Pixel.y * u_Size.x);
64
  uint  curr     = u_Records[index];
65

    
66
  if (curr != 0u)
67
    {
68
    const float S= 2147483647.0;
69
    uint depth = u_Records[curr+1u];
70
    uint texdepthuint = uint(S*(1.0-texdepth)/2.0);
71
    texdepth = 1.0 -2.0*float(depth)/S;
72

    
73
    if( depth >= texdepthuint )
74
      {
75
      vec4 clr= convert(u_Records[curr+2u]);
76
      curr = u_Records[curr];
77

    
78
      while (curr > 0u)
79
        {
80
        depth= u_Records[curr+1u];                       // keep walking the linked list
81
        if( depth < texdepthuint ) break;                // until we reach scene depth
82
        clr= blend( clr, convert(u_Records[curr+2u]) );  // and blending the colors in
83
        curr = u_Records[curr];
84
        }
85

    
86
      color = blend( clr, color);
87
      }
88
    }
89

    
90
  gl_FragDepth = texdepth;
91
  fragColor    = color;
92
  }
(9-9/10)