Project

General

Profile

« Previous | Next » 

Revision a13dde77

Added by Leszek Koltunski almost 6 years ago

Progress with a more generic 'preprocess' stage of Postprocessing Effects.

View differences:

src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
19 19

  
20 20
package org.distorted.library.main;
21 21

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

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

  
33
import java.io.InputStream;
27 34

  
28 35
///////////////////////////////////////////////////////////////////////////////////////////////////
29 36

  
......
34 41

  
35 42
  private int mHalo;
36 43

  
44
  static DistortedProgram mPreProgram;
45
  static int mPreColorH;
46

  
37 47
///////////////////////////////////////////////////////////////////////////////////////////////////
38 48

  
39 49
  EffectQueuePostprocess(long id)
......
78 88
    }
79 89

  
80 90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
  /**
92
   * Only for use by the library itself.
93
   *
94
   * @y.exclude
95
   */
96
  public static void createPrograms(Resources resources)
97
    {
98
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
99
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
100

  
101
    int numV = VertexEffect.getNumEnabled();
102

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

  
106
    String enabledEffectV= VertexEffect.getGLSL();
107

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

  
119
    int preProgramH = mPreProgram.getProgramHandle();
120
    EffectQueueVertex.getUniforms( preProgramH,2 );
121
    EffectQueueMatrix.getUniforms( preProgramH,2 );
122
    mPreColorH= GLES31.glGetUniformLocation( preProgramH, "u_Color");
123
    }
124

  
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
// TODO
81 127

  
82
  int getHalo()
128
  private boolean getWriteColor()
83 129
    {
84
    return mNumEffects>0 ? mHalo : 0;
130
    return false;
85 131
    }
86 132

  
87 133
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// TODO  (now only really works in case of 1 effect!)
89 134

  
90
  int getQuality()
135
  private int getHalo()
91 136
    {
92
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
137
    return mNumEffects>0 ? mHalo : 0;
93 138
    }
94 139

  
95 140
///////////////////////////////////////////////////////////////////////////////////////////////////
......
101 146
    }
102 147

  
103 148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// TODO  (now only really works in case of 1 effect!)
104 150

  
105
  int preprocess(DistortedOutputSurface[] buffers, DistortedNode node, long time)
151
  int getQuality()
152
    {
153
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
154
    }
155

  
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

  
158
  int preprocess(DistortedOutputSurface[] buffers, DistortedNode node)
106 159
    {
107
    int numRenders = 0;
108 160
    int quality = getInternalQuality();
161
    DistortedOutputSurface surface = buffers[quality];
162

  
163
    surface.setAsOutput();
164
    DistortedSurface input = node.getInternalSurface();
109 165

  
110
    buffers[quality].setAsOutput();
166
    if( input.setAsInput() )
167
      {
168
      MeshObject    mesh = node.getMesh();
169
      boolean writeColor = getWriteColor();
170
      float       margin = getHalo()*surface.mMipmap;
111 171

  
112
    numRenders += node.markStencilAndDepth(time,buffers[quality],this);
172
      float halfW = input.getWidth() / 2.0f;
173
      float halfH = input.getHeight()/ 2.0f;
174
      float halfZ = halfW*mesh.zFactor;
113 175

  
114
    return numRenders;
176
      DistortedRenderState.setUpStencilMark(writeColor);
177

  
178
      GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
179

  
180
      mPreProgram.useProgram();
181

  
182
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
183
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
184
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
185
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
186
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
187

  
188
      node.getEffects().send(halfW, halfH, halfZ, margin, surface, 2);
189

  
190
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
191

  
192
      DistortedRenderState.unsetUpStencilMark();
193

  
194
      return 1;
195
      }
196
    return 0;
115 197
    }
116 198

  
117 199
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff