Project

General

Profile

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

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

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
/**
32
 * Postprocessing Effect - an Effect that works by running a certain Shader Program(s) on a Framebuffer.
33
 */
34
public abstract class PostprocessEffect extends Effect
35
  {
36
/**
37
 * 5: 5 per-effect interpolated values.
38
 */
39
  public static final int NUM_UNIFORMS = 5;
40

    
41
  static final int POS_DATA_SIZE= 2;
42
  static final int TEX_DATA_SIZE= 2;
43

    
44
  static final FloatBuffer mQuadPositions, mQuadTexture, mQuadTextureInv;
45

    
46
  static
47
    {
48
    int dataLength      = 4;
49
    int bytes_per_float = 4;
50

    
51
    float[] position  = { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
52
    float[] textureNor= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
53
    float[] textureInv= {  0.0f,  0.0f,   1.0f, 0.0f,  0.0f, 1.0f,  1.0f, 1.0f };
54

    
55
    mQuadPositions = ByteBuffer.allocateDirect(POS_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
56
    mQuadPositions.put(position).position(0);
57
    mQuadTexture= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
58
    mQuadTexture.put(textureNor).position(0);
59
    mQuadTextureInv= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
60
    mQuadTextureInv.put(textureInv).position(0);
61
    }
62

    
63
  private static class Source
64
    {
65
    private String mName, mVertexShader, mFragmentShader;
66

    
67
    public Source(String name, String vertex, String fragment)
68
      {
69
      mName           = name;
70
      mVertexShader   = vertex;
71
      mFragmentShader = fragment;
72
      }
73
    }
74

    
75
  static ArrayList<DistortedProgram> mPrograms = new ArrayList<>();
76
  private static ArrayList<Source> mSources = new ArrayList<>();
77
  private static int mNumSources = 0;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  static int register(final String name, final String vertexShader, final String fragmentShader)
82
    {
83
    mSources.add(new Source(name,vertexShader,fragmentShader));
84

    
85
    return mNumSources++;
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
/**
90
 * Only for use by the library itself.
91
 *
92
 * @y.exclude
93
 */
94
  public static void createPrograms()
95
    {
96
    Source source;
97
    int len = mSources.size();
98

    
99
    for(int i=0; i<len; i++)
100
      {
101
      source = mSources.remove(0);
102

    
103
      //android.util.Log.d("postprocess", "compiling: "+source.mName);
104

    
105
      try
106
        {
107
        mPrograms.add (new DistortedProgram(source.mVertexShader,source.mFragmentShader));
108
        }
109
      catch(Exception e)
110
        {
111
        android.util.Log.e("Effects", "exception trying to compile "+source.mName+" program: "+e.getMessage());
112
        throw new RuntimeException(e.getMessage());
113
        }
114
      }
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
/**
119
 * Only for use by the library itself.
120
 *
121
 * @y.exclude
122
 */
123
  public abstract int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer);
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  PostprocessEffect(EffectName name)
128
    {
129
    super(name);
130
    }
131
  }
(17-17/26)