Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / PostprocessEffect.java @ aa2f0486

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 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
package org.distorted.library.effect;
21

    
22
import org.distorted.library.main.DistortedFramebuffer;
23
import org.distorted.library.program.DistortedProgram;
24

    
25
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27
import java.nio.FloatBuffer;
28
import java.util.ArrayList;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
// POSTPROCESSING EFFECTS.
32

    
33
public abstract class PostprocessEffect extends Effect
34
  {
35
  public static final int NUM_UNIFORMS = 5; // 5 per-effect interpolated values.
36

    
37
  static final int POS_DATA_SIZE= 2; // Blur Program: size of the position data in elements
38
  static final int TEX_DATA_SIZE= 2; // Blur Program: size of the texture coordinate data in elements.
39

    
40
  static final FloatBuffer mQuadPositions, mQuadTexture, mQuadTextureInv;
41

    
42
  static
43
    {
44
    int dataLength      = 4;
45
    int bytes_per_float = 4;
46

    
47
    float[] position  = { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
48
    float[] textureNor= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
49
    float[] textureInv= {  0.0f,  0.0f,   1.0f, 0.0f,  0.0f, 1.0f,  1.0f, 1.0f };
50

    
51
    mQuadPositions = ByteBuffer.allocateDirect(POS_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
52
    mQuadPositions.put(position).position(0);
53
    mQuadTexture= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
54
    mQuadTexture.put(textureNor).position(0);
55
    mQuadTextureInv= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
56
    mQuadTextureInv.put(textureInv).position(0);
57
    }
58

    
59
  private static class Source
60
    {
61
    private String mName, mVertexShader, mFragmentShader;
62

    
63
    public Source(String name, String vertex, String fragment)
64
      {
65
      mName           = name;
66
      mVertexShader   = vertex;
67
      mFragmentShader = fragment;
68
      }
69
    }
70

    
71
  static ArrayList<DistortedProgram> mPrograms = new ArrayList<>();
72
  private static ArrayList<Source> mSources = new ArrayList<>();
73
  private static int mNumSources = 0;
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  static int register(final String name, final String vertexShader, final String fragmentShader)
78
    {
79
    mSources.add(new Source(name,vertexShader,fragmentShader));
80

    
81
    return mNumSources++;
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  public static void createPrograms()
87
    {
88
    Source source;
89
    int len = mSources.size();
90

    
91
    for(int i=0; i<len; i++)
92
      {
93
      source = mSources.remove(0);
94

    
95
      //android.util.Log.d("postprocess", "compilaing: "+source.mName);
96

    
97
      try
98
        {
99
        mPrograms.add (new DistortedProgram(source.mVertexShader,source.mFragmentShader));
100
        }
101
      catch(Exception e)
102
        {
103
        android.util.Log.e("Effects", "exception trying to compile "+source.mName+" program: "+e.getMessage());
104
        throw new RuntimeException(e.getMessage());
105
        }
106
      }
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  public abstract int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer);
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  PostprocessEffect(EffectName name)
116
    {
117
    super(name);
118
    }
119
  }
(16-16/25)