Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueuePostprocess.java @ 247d8225

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 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.main;
21

    
22
import android.content.res.Resources;
23
import android.opengl.GLES31;
24
import android.util.Log;
25

    
26
import org.distorted.library.R;
27
import org.distorted.library.effect.EffectType;
28
import org.distorted.library.effect.PostprocessEffect;
29
import org.distorted.library.effect.VertexEffect;
30
import org.distorted.library.message.EffectMessage;
31
import org.distorted.library.program.DistortedProgram;
32

    
33
import java.io.InputStream;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
class EffectQueuePostprocess extends EffectQueue
38
  {
39
  private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
40
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
41

    
42
  private int mHalo;
43

    
44
  private static DistortedProgram mPreProgram;
45
  private static int mPreColorH;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  EffectQueuePostprocess(long id)
50
    { 
51
    super(id,NUM_UNIFORMS,INDEX );
52
    }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  void compute(long currTime)
57
    {
58
    if( currTime==mTime ) return;
59
    if( mTime==0 ) mTime = currTime;
60
    long step = (currTime-mTime);
61

    
62
    mHalo = 0;
63
    int halo;
64

    
65
    for(int i=0; i<mNumEffects; i++)
66
      {
67
      mCurrentDuration[i] += step;
68

    
69
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
70
        {
71
        for(int j=0; j<mNumListeners; j++)
72
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
73

    
74
        if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
75
          {
76
          remove(i--);
77
          mNumEffectsToBe--;
78
          regenerateIDandSort();
79
          continue;
80
          }
81
        }
82

    
83
      halo = (int)mUniforms[NUM_UNIFORMS*i];
84
      if( halo>mHalo ) mHalo = halo;
85
      }
86

    
87
    mTime = currTime;
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  static void createPrograms(Resources resources)
93
    {
94
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
95
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
96

    
97
    int numV = VertexEffect.getNumEnabled();
98

    
99
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? DistortedEffects.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
100
    String mainFragHeader= Distorted.GLSL_VERSION + "\n";
101

    
102
    String enabledEffectV= VertexEffect.getGLSL();
103

    
104
    try
105
      {
106
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
107
                                         enabledEffectV, null, Distorted.GLSL, null);
108
      }
109
    catch(Exception e)
110
      {
111
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
112
      throw new RuntimeException(e.getMessage());
113
      }
114

    
115
    int preProgramH = mPreProgram.getProgramHandle();
116
    EffectQueueVertex.getUniforms( preProgramH,2 );
117
    EffectQueueMatrix.getUniforms( preProgramH,2 );
118
    mPreColorH= GLES31.glGetUniformLocation( preProgramH, "u_Color");
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
// TODO
123

    
124
  private int getWriteColor()
125
    {
126
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getWriteColor() : 0;
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  private int getHalo()
132
    {
133
    return mNumEffects>0 ? mHalo : 0;
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// TODO  (now only really works in case of 1 effect!)
138

    
139
  private int getInternalQuality()
140
    {
141
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getInternalQuality() : 0;
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
// TODO  (now only really works in case of 1 effect!)
146

    
147
  int getQuality()
148
    {
149
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  int preprocess(DistortedOutputSurface[] buffers, DistortedNode node)
155
    {
156
    int quality = getInternalQuality();
157
    DistortedOutputSurface surface = buffers[quality];
158

    
159
    surface.setAsOutput();
160
    DistortedSurface input = node.getInternalSurface();
161

    
162
    if( input.setAsInput() )
163
      {
164
      MeshObject mesh = node.getMesh();
165
      int  writeColor = getWriteColor();
166
      float    margin = getHalo()*surface.mMipmap;
167

    
168
      float halfW = input.getWidth() / 2.0f;
169
      float halfH = input.getHeight()/ 2.0f;
170
      float halfZ = halfW*mesh.zFactor;
171

    
172
      DistortedRenderState.setUpStencilMark(writeColor!=0);
173

    
174
      GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
175

    
176
      mPreProgram.useProgram();
177

    
178
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
179
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
180
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
181
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
182
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
183

    
184
      node.getEffects().send(halfW, halfH, halfZ, margin, surface, 2);
185

    
186
      float a = (writeColor&255);
187

    
188
      if( a!=0.0f )
189
        {
190
        writeColor = (writeColor>>8);
191
        float b = writeColor&255;
192
        writeColor = (writeColor>>8);
193
        float g = writeColor&255;
194
        writeColor = (writeColor>>8);
195
        float r = writeColor&255;
196

    
197
        GLES31.glUniform4f(mPreColorH, r, g, b, a);
198
        }
199

    
200
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
201

    
202
      DistortedRenderState.unsetUpStencilMark();
203

    
204
      return 1;
205
      }
206
    return 0;
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  int postprocess(DistortedOutputSurface[] buffers)
212
    {
213
    int numRenders = 0;
214

    
215
    GLES31.glDisable(GLES31.GL_BLEND);
216

    
217
    for(int i=0; i<mNumEffects; i++)
218
      {
219
      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffers);
220
      }
221

    
222
    GLES31.glEnable(GLES31.GL_BLEND);
223

    
224
    return numRenders;
225
    }
226
  }
(16-16/20)