Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 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

    
21
precision highp float;
22
precision highp int;
23

    
24
out vec4 fragColor;           // The output color
25
in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
26
in vec2 v_Pixel;              // location of the current fragment, in pixels
27

    
28
//////////////////////////////////////////////////////////////////////////////////////////////
29
// per-pixel linked list. Order Independent Transparency.
30

    
31
uniform uvec2 u_Size;
32

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

    
39
//////////////////////////////////////////////////////////////////////////////////////////////
40

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

    
46
//////////////////////////////////////////////////////////////////////////////////////////////
47
// A over B (https://en.wikipedia.org/wiki/Alpha_compositing)
48

    
49
vec4 blend(vec4 A,vec4 B)
50
  {
51
  float b = B.a * (1.0-A.a);
52
  float a = A.a + b;
53

    
54
  return vec4( (A.rgb*A.a + B.rgb*b)/a , a );
55
  }
56

    
57
//////////////////////////////////////////////////////////////////////////////////////////////
58
// Pass4 of the OIT algorithm - keep traversing the linked list, build the final color and blend it.
59

    
60
void main()                    		
61
  {
62
  uint prev = uint(v_Pixel.x) + uint(v_Pixel.y) * u_Size.x;
63
  uint curr = u_Records[prev];
64

    
65
  if (curr != 0u)
66
    {
67
    const float S= 2147483647.0;
68
    gl_FragDepth = 1.0 - 2.0*float(u_Records[curr+1u])/S;
69
    vec4 color   = convert(u_Records[curr+2u]);
70
    curr = u_Records[curr];
71

    
72
    while (curr != 0u)
73
      {
74
      color = blend( color , convert(u_Records[curr+2u]) );
75
      curr = u_Records[curr];
76
      }
77

    
78
    fragColor = color;
79
    }
80
  else discard;
81
  }
(12-12/14)