Revision aa2f0486
Added by Leszek Koltunski over 8 years ago
| src/main/java/org/distorted/library/effect/PostprocessEffect.java | ||
|---|---|---|
| 20 | 20 |
package org.distorted.library.effect; |
| 21 | 21 |
|
| 22 | 22 |
import org.distorted.library.main.DistortedFramebuffer; |
| 23 |
import org.distorted.library.program.DistortedProgram; |
|
| 23 | 24 |
|
| 24 | 25 |
import java.nio.ByteBuffer; |
| 25 | 26 |
import java.nio.ByteOrder; |
| 26 | 27 |
import java.nio.FloatBuffer; |
| 28 |
import java.util.ArrayList; |
|
| 27 | 29 |
|
| 28 | 30 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 29 | 31 |
// POSTPROCESSING EFFECTS. |
| ... | ... | |
| 54 | 56 |
mQuadTextureInv.put(textureInv).position(0); |
| 55 | 57 |
} |
| 56 | 58 |
|
| 59 |
private static class Source |
|
| 60 |
{
|
|
| 61 |
private String mName, mVertexShader, mFragmentShader; |
|
| 62 |
|
|
| 63 |
public Source(String name, String vertex, String fragment) |
|
| 64 |
{
|
|
| 65 |
mName = name; |
|
| 66 |
mVertexShader = vertex; |
|
| 67 |
mFragmentShader = fragment; |
|
| 68 |
} |
|
| 69 |
} |
|
| 70 |
|
|
| 71 |
static ArrayList<DistortedProgram> mPrograms = new ArrayList<>(); |
|
| 72 |
private static ArrayList<Source> mSources = new ArrayList<>(); |
|
| 73 |
private static int mNumSources = 0; |
|
| 74 |
|
|
| 75 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 76 |
|
|
| 77 |
static int register(final String name, final String vertexShader, final String fragmentShader) |
|
| 78 |
{
|
|
| 79 |
mSources.add(new Source(name,vertexShader,fragmentShader)); |
|
| 80 |
|
|
| 81 |
return mNumSources++; |
|
| 82 |
} |
|
| 83 |
|
|
| 84 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 85 |
|
|
| 86 |
public static void createPrograms() |
|
| 87 |
{
|
|
| 88 |
Source source; |
|
| 89 |
int len = mSources.size(); |
|
| 90 |
|
|
| 91 |
for(int i=0; i<len; i++) |
|
| 92 |
{
|
|
| 93 |
source = mSources.remove(0); |
|
| 94 |
|
|
| 95 |
//android.util.Log.d("postprocess", "compilaing: "+source.mName);
|
|
| 96 |
|
|
| 97 |
try |
|
| 98 |
{
|
|
| 99 |
mPrograms.add (new DistortedProgram(source.mVertexShader,source.mFragmentShader)); |
|
| 100 |
} |
|
| 101 |
catch(Exception e) |
|
| 102 |
{
|
|
| 103 |
android.util.Log.e("Effects", "exception trying to compile "+source.mName+" program: "+e.getMessage());
|
|
| 104 |
throw new RuntimeException(e.getMessage()); |
|
| 105 |
} |
|
| 106 |
} |
|
| 107 |
} |
|
| 108 |
|
|
| 57 | 109 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 58 | 110 |
|
| 59 | 111 |
public abstract int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer); |
| src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java | ||
|---|---|---|
| 56 | 56 |
// We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4) |
| 57 | 57 |
private static float[] weightsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4]; |
| 58 | 58 |
private static float[] offsetsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4]; |
| 59 |
|
|
| 60 |
private static DistortedProgram mBlur1Program, mBlur2Program; |
|
| 61 | 59 |
private static float[] mWeights = new float[MAX_HALO]; |
| 62 | 60 |
private static float[] mOffsets = new float[MAX_HALO]; |
| 63 | 61 |
|
| 62 |
private static DistortedProgram mBlur1Program, mBlur2Program; |
|
| 63 |
private static int mBlur1Index, mBlur2Index; |
|
| 64 |
|
|
| 64 | 65 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 65 | 66 |
/** |
| 66 | 67 |
* Blur the object. |
| ... | ... | |
| 85 | 86 |
|
| 86 | 87 |
public int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer) |
| 87 | 88 |
{
|
| 89 |
if( mBlur1Program==null) |
|
| 90 |
{
|
|
| 91 |
mBlur1Program = mPrograms.get(mBlur1Index); |
|
| 92 |
mBlur2Program = mPrograms.get(mBlur2Index); |
|
| 93 |
} |
|
| 94 |
|
|
| 88 | 95 |
buffer.setAsOutput(); |
| 89 | 96 |
|
| 90 | 97 |
float w1 = buffer.getWidth(); |
| ... | ... | |
| 149 | 156 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 150 | 157 |
|
| 151 | 158 |
public static void enable() |
| 152 |
{
|
|
| 153 |
} |
|
| 154 |
|
|
| 155 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 156 |
|
|
| 157 |
public static void createProgram() |
|
| 158 | 159 |
{
|
| 159 | 160 |
final String blurVertex = |
| 160 | 161 |
|
| ... | ... | |
| 217 | 218 |
"fragColor = pixel; \n"+ |
| 218 | 219 |
"}"; |
| 219 | 220 |
|
| 220 |
try |
|
| 221 |
{
|
|
| 222 |
mBlur1Program = new DistortedProgram(blurVertex,blurFragment1); |
|
| 223 |
} |
|
| 224 |
catch(Exception e) |
|
| 225 |
{
|
|
| 226 |
android.util.Log.e("Effects", "exception trying to compile BLUR1 program: "+e.getMessage());
|
|
| 227 |
throw new RuntimeException(e.getMessage()); |
|
| 228 |
} |
|
| 229 |
|
|
| 230 |
try |
|
| 231 |
{
|
|
| 232 |
mBlur2Program = new DistortedProgram(blurVertex,blurFragment2); |
|
| 233 |
} |
|
| 234 |
catch(Exception e) |
|
| 235 |
{
|
|
| 236 |
android.util.Log.e("Effects", "exception trying to compile BLUR2 program: "+e.getMessage());
|
|
| 237 |
throw new RuntimeException(e.getMessage()); |
|
| 238 |
} |
|
| 221 |
mBlur1Index = PostprocessEffect.register("BLUR1", blurVertex,blurFragment1);
|
|
| 222 |
mBlur2Index = PostprocessEffect.register("BLUR2", blurVertex,blurFragment2);
|
|
| 239 | 223 |
} |
| 240 | 224 |
|
| 241 | 225 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| src/main/java/org/distorted/library/main/Distorted.java | ||
|---|---|---|
| 26 | 26 |
|
| 27 | 27 |
import org.distorted.library.effect.Effect; |
| 28 | 28 |
import org.distorted.library.effect.FragmentEffect; |
| 29 |
import org.distorted.library.effect.PostprocessEffectBlur;
|
|
| 29 |
import org.distorted.library.effect.PostprocessEffect; |
|
| 30 | 30 |
import org.distorted.library.effect.VertexEffect; |
| 31 | 31 |
import org.distorted.library.program.*; |
| 32 | 32 |
|
| ... | ... | |
| 124 | 124 |
|
| 125 | 125 |
final Resources resources = context.getResources(); |
| 126 | 126 |
DistortedEffects.createProgram(resources); |
| 127 |
PostprocessEffectBlur.createProgram();
|
|
| 127 |
PostprocessEffect.createPrograms();
|
|
| 128 | 128 |
EffectMessageSender.startSending(); |
| 129 | 129 |
|
| 130 | 130 |
mInitialized = true; |
| src/main/java/org/distorted/library/program/DistortedProgram.java | ||
|---|---|---|
| 224 | 224 |
|
| 225 | 225 |
if( uniform!=null ) |
| 226 | 226 |
{
|
| 227 |
android.util.Log.d("program", "new uniform: "+uniform);
|
|
| 227 |
//android.util.Log.d("program", "new uniform: "+uniform);
|
|
| 228 | 228 |
if( mUniList.length()>0 ) mUniList+=" "; |
| 229 | 229 |
mUniList += uniform; |
| 230 | 230 |
} |
Also available in: Unified diff
Now all PostprocessEffects are truly self-contained, including dynamic enable() and all shader sources.