Project

General

Profile

« Previous | Next » 

Revision 03cb451d

Added by Leszek Koltunski over 7 years ago

Speed up shaders (both compilation and execution) by explicitly enabling only the needed effects.

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
55 55
  private static final int TEX_DATA_SIZE     = 2; // Main Program: size of the texture coordinate data in elements.
56 56

  
57 57
  private static DistortedProgram mProgram;
58
  private static boolean[] mEffectEnabled = new boolean[EffectNames.size()];
59

  
60
  static
61
    {
62
    int len = EffectNames.size();
63

  
64
    for(int i=0; i<len; i++)
65
      {
66
      mEffectEnabled[i] = false;
67
      }
68
    }
58 69

  
59 70
  /// DEBUG ONLY /////
60 71
  private static DistortedProgram mDebugProgram;
......
91 102
  static void createProgram(Resources resources)
92 103
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
93 104
    {
94
    final InputStream mainVertexStream   = resources.openRawResource(R.raw.main_vertex_shader);
95
    final InputStream mainFragmentStream = resources.openRawResource(R.raw.main_fragment_shader);
105
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
106
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
107

  
108
    String mainVertHeader= ("#version 100\n");
109
    String mainFragHeader= ("#version 100\n");
96 110

  
97
    String mainVertexHeader= ("#version 100\n#define NUM_VERTEX "  + getMaxVertex()+"\n");
111
    EffectNames name;
112
    EffectTypes type;
113
    boolean foundF = false;
114
    boolean foundV = false;
98 115

  
99
    for(EffectNames name: EffectNames.values() )
116
    for(int i=0; i<mEffectEnabled.length; i++)
100 117
      {
101
      if( name.getType()== EffectTypes.VERTEX)
102
        mainVertexHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
118
      if( mEffectEnabled[i] )
119
        {
120
        name = EffectNames.getName(i);
121
        type = EffectNames.getType(i);
122

  
123
        if( type == EffectTypes.VERTEX )
124
          {
125
          mainVertHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
126
          foundV = true;
127
          }
128
        else if( type == EffectTypes.FRAGMENT )
129
          {
130
          mainFragHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
131
          foundF = true;
132
          }
133
        }
103 134
      }
104 135

  
105
    String mainFragmentHeader= ("#version 100\n#define NUM_FRAGMENT "  + getMaxFragment()+"\n");
136
    mainVertHeader += ("#define NUM_VERTEX "   + ( foundV ? getMaxVertex()   : 0 ) + "\n");
137
    mainFragHeader += ("#define NUM_FRAGMENT " + ( foundF ? getMaxFragment() : 0 ) + "\n");
106 138

  
107
    for(EffectNames name: EffectNames.values() )
108
      {
109
      if( name.getType()== EffectTypes.FRAGMENT)
110
        mainFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
111
      }
139
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
140
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
112 141

  
113
    mProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
142
    mProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader);
114 143

  
115 144
    int mainProgramH = mProgram.getProgramHandle();
116 145
    EffectQueueFragment.getUniforms(mainProgramH);
......
332 361
  static void onDestroy()
333 362
    {
334 363
    mNextID = 0;
364

  
365
    int len = EffectNames.size();
366

  
367
    for(int i=0; i<len; i++)
368
      {
369
      mEffectEnabled[i] = false;
370
      }
335 371
    }
336 372

  
337 373
///////////////////////////////////////////////////////////////////////////////////////////////////
......
498 534
    return false;
499 535
    }
500 536

  
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538
/**
539
 * Enables a given Effect.
540
 * <p>
541
 * By default, all effects are disabled. One has to explicitly enable each effect one intends to use.
542
 * This needs to be called BEFORE shaders get compiled, i.e. before the call to Distorted.onCreate().
543
 * The point: by enabling only the effects we need, we can optimize the shaders.
544
 *
545
 * @param name Name of the Effect to enable.
546
 */
547
  public static void enableEffect(EffectNames name)
548
    {
549
    mEffectEnabled[name.ordinal()] = true;
550
    }
551

  
501 552
///////////////////////////////////////////////////////////////////////////////////////////////////
502 553
/**
503 554
 * Returns the maximum number of Matrix effects.
src/main/java/org/distorted/library/EffectNames.java
372 372
    return false;
373 373
    }
374 374

  
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

  
377
  static int size()
378
    {
379
    return values().length;
380
    }
381

  
375 382
///////////////////////////////////////////////////////////////////////////////////////////////////
376 383
// PUBLIC API
377 384
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/res/raw/main_fragment_shader.glsl
85 85
  }
86 86
#endif
87 87

  
88
#endif
88
#endif    // NUM_FRAGMENT>0
89 89

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

  
......
96 96
#if NUM_FRAGMENT>0
97 97
  vec2 diff;
98 98
  float pointDegree;
99
  
99
  int j=0;
100

  
100 101
  for(int i=0; i<fNumEffects; i++)
101 102
    {
102
    diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
103
    diff = (v_Position.xy - fUniforms[j+1].xy)/fUniforms[j+1].zw;
103 104
    pointDegree = max(0.0,1.0-dot(diff,diff));
104 105

  
105 106
#ifdef CHROMA
106
    if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),2*i,pixel); else
107
    if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),j,pixel); else
107 108
#endif
108 109
#ifdef SMOOTH_CHROMA
109
    if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,2*i,pixel); else
110
    if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,j,pixel); else
110 111
#endif
111 112
#ifdef ALPHA
112
    if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),2*i,pixel); else
113
    if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),j,pixel); else
113 114
#endif
114 115
#ifdef SMOOTH_ALPHA
115
    if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,2*i,pixel); else
116
    if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,j,pixel); else
116 117
#endif
117 118
#ifdef BRIGHTNESS
118
    if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),2*i,pixel); else
119
    if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),j,pixel); else
119 120
#endif
120 121
#ifdef SMOOTH_BRIGHTNESS
121
    if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,2*i,pixel); else
122
    if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,j,pixel); else
122 123
#endif
123 124
#ifdef CONTRAST
124
    if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),2*i,pixel); else
125
    if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),j,pixel); else
125 126
#endif
126 127
#ifdef SMOOTH_CONTRAST
127
    if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,2*i,pixel); else
128
    if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,j,pixel); else
128 129
#endif
129 130
#ifdef SATURATION
130
    if( fType[i]==SATURATION        ) saturation(sign(pointDegree),2*i,pixel); else
131
    if( fType[i]==SATURATION        ) saturation(sign(pointDegree),j,pixel); else
131 132
#endif
132 133
#ifdef SMOOTH_SATURATION
133
    if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,2*i,pixel); else
134
    if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,j,pixel); else
134 135
#endif
135 136
    {}
137

  
138
    j+=2;
136 139
    }
137 140
#endif
138 141

  

Also available in: Unified diff