Project

General

Profile

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

library / src / main / res / raw / oit_render_fragment_shader.glsl @ 33f59f22

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
//////////////////////////////////////////////////////////////////////////////////////////////
28
// per-pixel linked list. Order Independent Transparency.
29

    
30
uniform vec2 u_Size;
31

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

    
38
//////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
vec4 convert(uint rgba)
41
  {
42
  return vec4( float((rgba>>24u)&255u),float((rgba>>16u)&255u),float((rgba>>8u)&255u),float(rgba&255u) ) / 255.0;
43
  }
44

    
45
//////////////////////////////////////////////////////////////////////////////////////////////
46
// https://en.wikipedia.org/wiki/Alpha_compositing (premultiplied)
47

    
48
vec4 blend(vec4 clr,vec4 srf)
49
  {
50
  return clr + (1.0 - clr.a) * vec4(srf.rgb * srf.a , srf.a);
51
  }
52

    
53
//////////////////////////////////////////////////////////////////////////////////////////////
54
// Pass4 of the OIT algorithm - keep traversing the linked list, build the final color and blend it.
55

    
56
void main()                    		
57
  {
58
  uint prev = uint(v_Pixel.x + v_Pixel.y * u_Size.x);
59
  uint curr = u_Records[prev];
60

    
61
  if (curr != 0u)
62
    {
63
    const float S= 2147483647.0;
64
    gl_FragDepth = 1.0 - 2.0*float(u_Records[curr+1u])/S;
65
    vec4 color   = convert(u_Records[curr+2u]);
66

    
67
    while (curr != 0u)
68
      {
69
      curr = u_Records[curr];
70
      color = blend( color , convert(u_Records[curr+2u]) );
71
      }
72

    
73
    fragColor = color;
74
    }
75
  else discard;
76
  }
(10-10/11)