Project

General

Profile

« Previous | Next » 

Revision 041b6dee

Added by Leszek Koltunski almost 7 years ago

Simplification in PostprocessEffects

View differences:

src/main/java/org/distorted/library/program/DistortedProgram.java
20 20
package org.distorted.library.program;
21 21

  
22 22
import android.opengl.GLES30;
23
import android.os.Build;
24

  
25
import org.distorted.library.effect.EffectType;
26
import org.distorted.library.main.DistortedEffects;
27 23

  
28 24
import java.io.BufferedReader;
29 25
import java.io.IOException;
......
36 32
 */
37 33
public class DistortedProgram
38 34
  {
39
  private String mAttributeStr;
40
  private int mAttributeLen;
35
  private String mAttributeStr, mUniformStr, mUniList;
36
  private int mAttributeLen, mUniformLen;
41 37

  
42 38
  private int mProgramHandle;
43 39
  private int mNumAttributes;
40
  private int mNumUniforms;
44 41
  private String[] mAttributeName;
42
  private String[] mUniformName;
45 43

  
46 44
  public final int[] mAttribute;
45
  public final int[] mUniform;
47 46

  
48 47
///////////////////////////////////////////////////////////////////////////////////////////////////
49 48

  
......
93 92
    return programHandle;
94 93
    }
95 94

  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

  
97
  private void init(int glslVersion)
98
    {
99
    mAttributeStr  = (glslVersion == 100 ? "attribute " : "in ");
100
    mAttributeLen  = mAttributeStr.length();
101
    mNumAttributes = 0;
102
    mUniformStr    = "uniform ";
103
    mUniformLen    = mUniformStr.length();
104
    mNumUniforms   = 0;
105
    mUniList       = "";
106
    }
107

  
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

  
110
  private String parseOutUniform(final String line)
111
    {
112
    int len = line.length();
113
    int whiteSpace, semicolon, nameBegin;
114
    char currChar;
115

  
116
    for(whiteSpace=0; whiteSpace<len; whiteSpace++)
117
      {
118
      currChar = line.charAt(whiteSpace);
119
      if( currChar!=' ' && currChar!='\t') break;
120
      }
121

  
122
    for(semicolon=whiteSpace; semicolon<len; semicolon++)
123
      {
124
      currChar = line.charAt(semicolon);
125
      if( currChar==';') break;
126
      }
127

  
128
    if( semicolon<len && semicolon-whiteSpace>=mUniformLen+1 )
129
      {
130
      String subline = line.substring(whiteSpace,semicolon);
131
      int subLen = semicolon-whiteSpace;
132

  
133
      if( subline.startsWith(mUniformStr))
134
        {
135
        //android.util.Log.e("program", "GOOD LINE: " +subline+" subLen="+subLen);
136

  
137
        for(nameBegin=subLen-1; nameBegin>mUniformLen-2; nameBegin--)
138
          {
139
          currChar=subline.charAt(nameBegin);
140

  
141
          if( currChar==' ' || currChar=='\t' )
142
            {
143
            mNumUniforms++;
144
            String uniform = subline.substring(nameBegin+1,subLen);
145
            int brace = uniform.indexOf("[");
146

  
147
            return brace>=0 ? uniform.substring(0,brace) : uniform;
148
            }
149
          }
150
        }
151
      }
152

  
153
    return null;
154
    }
155

  
96 156
///////////////////////////////////////////////////////////////////////////////////////////////////
97 157

  
98 158
  private String parseOutAttribute(final String line)
......
138 198
    return null;
139 199
    }
140 200

  
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

  
203
  private void doAttributes(final String shader, boolean doAttributes)
204
    {
205
    String attribute, attrList="", uniform;
206
    String[] lines = shader.split("\n");
207
    int length = lines.length;
208

  
209
    for(int i=0; i<length; i++)
210
      {
211
      if( doAttributes )
212
        {
213
        attribute = parseOutAttribute(lines[i]);
214

  
215
        if( attribute!=null )
216
          {
217
          //android.util.Log.d("program", "new attribute: "+attribute);
218
          if( attrList.length()>0 ) attrList+=" ";
219
          attrList += attribute;
220
          }
221
        }
222

  
223
      uniform = parseOutUniform(lines[i]);
224

  
225
      if( uniform!=null )
226
        {
227
        android.util.Log.d("program", "new uniform: "+uniform);
228
        if( mUniList.length()>0 ) mUniList+=" ";
229
        mUniList += uniform;
230
        }
231
      }
232

  
233
    if( doAttributes ) mAttributeName = attrList.split(" ");
234
    }
235

  
141 236
///////////////////////////////////////////////////////////////////////////////////////////////////
142 237

  
143 238
  private String readTextFileFromRawResource(final InputStream inputStream, boolean doAttributes)
......
173 268
      return null;
174 269
      }
175 270

  
176
    if( doAttributes )
177
      {
178
      mAttributeName = attrList.split(" ");
179
      }
271
    if( doAttributes ) mAttributeName = attrList.split(" ");
180 272

  
181 273
    return body.toString();
182 274
    }
183 275

  
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

  
186
  private static void sanitizeMaxValues()
187
  throws VertexUniformsException,FragmentUniformsException
188
    {
189
    int maxV,maxF;
190
    int[] param = new int[1];
191

  
192
    GLES30.glGetIntegerv(GLES30.GL_MAX_VERTEX_UNIFORM_VECTORS, param, 0);
193
    maxV = param[0];
194
    GLES30.glGetIntegerv(GLES30.GL_MAX_FRAGMENT_UNIFORM_VECTORS, param, 0);
195
    maxF = param[0];
196

  
197
    //Log.d("program", "Max vectors in vertex shader: "+maxV);
198
    //Log.d("program", "Max vectors in fragment shader: "+maxF);
199

  
200
    if( !Build.FINGERPRINT.contains("generic") )
201
      {
202
      int realMaxV = (maxV-11)/4;   // adjust this in case of changes to the shaders...
203
      int realMaxF = (maxF- 2)/4;   //
204

  
205
      if( DistortedEffects.getMax(EffectType.VERTEX)   > realMaxV )
206
        {
207
        throw new VertexUniformsException("Too many effects in the vertex shader, max is "+realMaxV, realMaxV);
208
        }
209
      if( DistortedEffects.getMax(EffectType.FRAGMENT) > realMaxF )
210
        {
211
        throw new FragmentUniformsException("Too many effects in the fragment shader, max is "+realMaxF, realMaxF);
212
        }
213
      }
214
    }
215

  
216 276
///////////////////////////////////////////////////////////////////////////////////////////////////
217 277

  
218 278
  private static int compileShader(final int shaderType, final String shaderSource)
......
288 348
  public DistortedProgram(final InputStream vertex, final InputStream fragment, final String vertexHeader, final String fragmentHeader, int glslVersion, final String[] feedback )
289 349
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
290 350
    {
291
    mAttributeStr = (glslVersion == 100 ? "attribute " : "in ");
292
    mAttributeLen = mAttributeStr.length();
293

  
294
    mNumAttributes = 0;
351
    init(glslVersion);
295 352

  
296 353
    final String vertexShader   = readTextFileFromRawResource(vertex  , true );
297 354
    final String fragmentShader = readTextFileFromRawResource(fragment, false);
298 355

  
299
    sanitizeMaxValues();
300

  
301 356
    final int vertexShaderHandle   = compileShader(GLES30.GL_VERTEX_SHADER  , vertexHeader   + vertexShader  );
302 357
    final int fragmentShaderHandle = compileShader(GLES30.GL_FRAGMENT_SHADER, fragmentHeader + fragmentShader);
303 358

  
......
309 364
      {
310 365
      mAttribute[i] = GLES30.glGetAttribLocation( mProgramHandle, mAttributeName[i]);
311 366
      }
367

  
368
    if( mNumUniforms>0 )
369
      {
370
      mUniform = new int[mNumUniforms];
371
      mUniformName = mUniList.split(" ");
372

  
373
      for(int i=0; i<mNumUniforms; i++)
374
        {
375
        mUniform[i] = GLES30.glGetUniformLocation( mProgramHandle, mUniformName[i]);
376
        }
377
      }
378
    else mUniform = null;
379
    }
380

  
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

  
383
  public DistortedProgram(final String vertex, final String fragment)
384
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
385
    {
386
    init(300);
387

  
388
    doAttributes(vertex  , true );
389
    doAttributes(fragment, false);
390

  
391
    final int vertexShaderHandle   = compileShader(GLES30.GL_VERTEX_SHADER  , vertex  );
392
    final int fragmentShaderHandle = compileShader(GLES30.GL_FRAGMENT_SHADER, fragment);
393

  
394
    mProgramHandle = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, mAttributeName, null );
395

  
396
    mAttribute = new int[mNumAttributes];
397

  
398
    for(int i=0; i<mNumAttributes; i++)
399
      {
400
      mAttribute[i] = GLES30.glGetAttribLocation( mProgramHandle, mAttributeName[i]);
401
      }
402

  
403
    if( mNumUniforms>0 )
404
      {
405
      mUniform = new int[mNumUniforms];
406
      mUniformName = mUniList.split(" ");
407

  
408
      for(int i=0; i<mNumUniforms; i++)
409
        {
410
        mUniform[i] = GLES30.glGetUniformLocation( mProgramHandle, mUniformName[i]);
411
        }
412
      }
413
    else mUniform = null;
312 414
    }
313 415

  
314 416
///////////////////////////////////////////////////////////////////////////////////////////////////
......
319 421
                          final String enabledVertex, final String enabledFragment, int glslVersion, final String[] feedback )
320 422
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
321 423
    {
322
    mAttributeStr = (glslVersion == 100 ? "attribute " : "in ");
323
    mAttributeLen = mAttributeStr.length();
324

  
325
    mNumAttributes = 0;
424
    init(glslVersion);
326 425

  
327 426
    String vertexShader   = readTextFileFromRawResource(vertex  , true );
328 427
    String fragmentShader = readTextFileFromRawResource(fragment, false);
......
330 429
    vertexShader   = insertEnabledEffects(vertexShader  ,enabledVertex  );
331 430
    fragmentShader = insertEnabledEffects(fragmentShader,enabledFragment);
332 431

  
333
    sanitizeMaxValues();
334

  
335 432
    final int vertexShaderHandle   = compileShader(GLES30.GL_VERTEX_SHADER  , vertexHeader   + vertexShader  );
336 433
    final int fragmentShaderHandle = compileShader(GLES30.GL_FRAGMENT_SHADER, fragmentHeader + fragmentShader);
337 434

  
......
343 440
      {
344 441
      mAttribute[i] = GLES30.glGetAttribLocation( mProgramHandle, mAttributeName[i]);
345 442
      }
443

  
444
    if( mNumUniforms>0 )
445
      {
446
      mUniform = new int[mNumUniforms];
447
      mUniformName = mUniList.split(" ");
448

  
449
      for(int i=0; i<mNumUniforms; i++)
450
        {
451
        mUniform[i] = GLES30.glGetUniformLocation( mProgramHandle, mUniformName[i]);
452
        }
453
      }
454
    else mUniform = null;
346 455
    }
347 456

  
348 457
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff