Project

General

Profile

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

library / src / main / res / raw / blit_depth_render_fragment_shader.glsl @ 47511918

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
#if __VERSION__ != 100
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
#define TEXTURE texture
28
#define FRAG_COLOR fragColor
29
#else
30
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
31
varying vec2 v_Pixel;         // location of the current fragment, in pixels
32
#define TEXTURE texture2D
33
#define FRAG_COLOR gl_FragColor
34
#endif
35

    
36
uniform sampler2D u_DepthTexture;
37

    
38
//////////////////////////////////////////////////////////////////////////////////////////////
39
// per-pixel linked list. Order Independent Transparency.
40

    
41
uniform ivec2 u_Size;
42

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

    
49
//////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
vec4 convert(uint rgba)
52
  {
53
  return vec4( float((rgba>>24u)&255u),float((rgba>>16u)&255u),float((rgba>>8u)&255u),float(rgba&255u) ) / 255.0;
54
  }
55

    
56
//////////////////////////////////////////////////////////////////////////////////////////////
57
// https://en.wikipedia.org/wiki/Alpha_compositing (premultiplied)
58

    
59
vec4 blend(vec4 clr,vec4 srf)
60
  {
61
  return clr + (1.0 - clr.a) * vec4(srf.rgb * srf.a , srf.a);
62
  }
63

    
64
//////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
void main()                    		
67
  {
68
  uint index = uint(v_Pixel.x + v_Pixel.y * float(u_Size.x));
69
  uint ptr = u_Records[index];
70

    
71
  if( ptr == 0u ) discard;
72
  else
73
    {
74
    u_Records[index] = 0u;
75
    vec4 color = convert(u_Records[ptr]);
76

    
77
         if( (4*int(v_Pixel.x) > u_Size.x) && color.r==1.0 ) FRAG_COLOR = color;
78
    else if( (4*int(v_Pixel.x) <=u_Size.x) && color.g==1.0 ) FRAG_COLOR = color;
79
    else FRAG_COLOR = vec4(0.0,0.0,1.0,1.0);
80
    //FRAG_COLOR = convert(u_Records[index]);
81
    //u_Records[index] = 0u;
82
    }
83

    
84
  /*
85
  uint index = uint(v_Pixel.x + v_Pixel.y * float(u_Size.x));
86
  uint curr = u_Records[index];
87

    
88
  if (curr == 0u) discard;
89

    
90
  vec4 color= vec4(0.0,0.0,0.0,0.0);
91
  u_Records[index] = 0u;
92

    
93
  while (curr > 0u)                                      // keep walking the linked list
94
    {                                                    // and blending the colors in
95
    curr = u_Records[curr];                              //
96
    //color= blend( color, convert(u_Records[curr+2u]) );  //
97

    
98
    color = convert(u_Records[curr+2u]);
99

    
100
    if( color.a == 0.0 ) color = vec4(0.0,1.0,0.0,1.0);
101
    }
102

    
103

    
104
  if( curr>0u )
105
    {
106
    curr = u_Records[curr];
107
    color= convert(u_Records[curr+2u]);
108
    }
109

    
110
  gl_FragDepth = TEXTURE(u_DepthTexture, v_TexCoordinate).r;
111
  FRAG_COLOR = color;
112

    
113
//else
114
//  FRAG_COLOR = vec4(1.0,0.0,0.0,1.0);
115
*/
116
  }
(2-2/9)