Revision 041b6dee
Added by Leszek Koltunski over 8 years ago
| src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java | ||
|---|---|---|
| 19 | 19 |
|
| 20 | 20 |
package org.distorted.library.effect; |
| 21 | 21 |
|
| 22 |
import android.content.res.Resources; |
|
| 23 | 22 |
import android.opengl.GLES30; |
| 24 | 23 |
|
| 25 |
import org.distorted.library.R; |
|
| 26 |
import org.distorted.library.main.Distorted; |
|
| 27 | 24 |
import org.distorted.library.main.DistortedFramebuffer; |
| 28 | 25 |
import org.distorted.library.main.DistortedRenderState; |
| 29 | 26 |
import org.distorted.library.program.DistortedProgram; |
| 30 |
import org.distorted.library.program.FragmentCompilationException; |
|
| 31 |
import org.distorted.library.program.FragmentUniformsException; |
|
| 32 |
import org.distorted.library.program.LinkingException; |
|
| 33 |
import org.distorted.library.program.VertexCompilationException; |
|
| 34 |
import org.distorted.library.program.VertexUniformsException; |
|
| 35 | 27 |
import org.distorted.library.type.Data1D; |
| 36 | 28 |
|
| 37 |
import java.io.InputStream; |
|
| 38 |
|
|
| 39 | 29 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 40 | 30 |
|
| 41 | 31 |
public class PostprocessEffectBlur extends PostprocessEffect |
| ... | ... | |
| 68 | 58 |
private static float[] offsetsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4]; |
| 69 | 59 |
|
| 70 | 60 |
private static DistortedProgram mBlur1Program, mBlur2Program; |
| 71 |
private static int mRadius1H,mOffsets1H,mWeights1H,mDepth1H, mColorTexture1H; |
|
| 72 |
private static int mRadius2H,mOffsets2H,mWeights2H,mDepth2H, mColorTexture2H; |
|
| 73 | 61 |
private static float[] mWeights = new float[MAX_HALO]; |
| 74 | 62 |
private static float[] mOffsets = new float[MAX_HALO]; |
| 75 | 63 |
|
| ... | ... | |
| 86 | 74 |
mBlurRadius = blurRadius; |
| 87 | 75 |
} |
| 88 | 76 |
|
| 89 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 90 |
|
|
| 91 |
public static void enable() |
|
| 92 |
{
|
|
| 93 |
|
|
| 94 |
} |
|
| 95 |
|
|
| 96 | 77 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 97 | 78 |
|
| 98 | 79 |
public boolean compute(float[] uniforms, int index, long currentDuration, long step ) |
| ... | ... | |
| 123 | 104 |
buffer.bindForOutput(1); |
| 124 | 105 |
buffer.setAsInput(0); |
| 125 | 106 |
|
| 126 |
GLES30.glUniform1fv( mWeights1H, radius+1, weightsCache,offset);
|
|
| 127 |
GLES30.glUniform1i( mRadius1H, radius);
|
|
| 128 |
GLES30.glUniform1f( mDepth1H , near);
|
|
| 129 |
GLES30.glUniform1i( mColorTexture1H , 0 );
|
|
| 107 |
GLES30.glUniform1fv( mBlur1Program.mUniform[3], radius+1, weightsCache,offset);
|
|
| 108 |
GLES30.glUniform1i( mBlur1Program.mUniform[4], radius);
|
|
| 109 |
GLES30.glUniform1f( mBlur1Program.mUniform[0] , near);
|
|
| 110 |
GLES30.glUniform1i( mBlur1Program.mUniform[1] , 0 );
|
|
| 130 | 111 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1; |
| 131 |
GLES30.glUniform1fv( mOffsets1H ,radius+1, mOffsets,0);
|
|
| 112 |
GLES30.glUniform1fv( mBlur1Program.mUniform[2] ,radius+1, mOffsets,0);
|
|
| 132 | 113 |
GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions); |
| 133 | 114 |
GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture); |
| 134 | 115 |
|
| ... | ... | |
| 147 | 128 |
GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f); |
| 148 | 129 |
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT); |
| 149 | 130 |
|
| 150 |
GLES30.glUniform1fv( mWeights2H, radius+1, weightsCache,offset);
|
|
| 151 |
GLES30.glUniform1i( mRadius2H, radius);
|
|
| 152 |
GLES30.glUniform1f( mDepth2H , near);
|
|
| 153 |
GLES30.glUniform1i( mColorTexture2H , 0 );
|
|
| 131 |
GLES30.glUniform1fv( mBlur2Program.mUniform[3], radius+1, weightsCache,offset);
|
|
| 132 |
GLES30.glUniform1i( mBlur2Program.mUniform[4], radius);
|
|
| 133 |
GLES30.glUniform1f( mBlur2Program.mUniform[0] , near);
|
|
| 134 |
GLES30.glUniform1i( mBlur2Program.mUniform[1] , 0 );
|
|
| 154 | 135 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1; |
| 155 |
GLES30.glUniform1fv( mOffsets2H ,radius+1, mOffsets,0);
|
|
| 136 |
GLES30.glUniform1fv( mBlur2Program.mUniform[2] ,radius+1, mOffsets,0);
|
|
| 156 | 137 |
GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions); |
| 157 | 138 |
GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture); |
| 158 | 139 |
|
| ... | ... | |
| 167 | 148 |
|
| 168 | 149 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 169 | 150 |
|
| 170 |
public static void createProgram(Resources resources) |
|
| 171 |
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException |
|
| 151 |
public static void enable() |
|
| 172 | 152 |
{
|
| 173 |
int gl = Distorted.getGlVersion();
|
|
| 174 |
int GLSL = (gl==3 ? 300:100); |
|
| 175 |
String GLSL_VERSION= (gl==3 ? "#version 300 es\n" : "#version 100\n");
|
|
| 153 |
}
|
|
| 154 |
|
|
| 155 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
| 176 | 156 |
|
| 177 |
final InputStream blur1VertexStream = resources.openRawResource(R.raw.blur_vertex_shader); |
|
| 178 |
final InputStream blur1FragmentStream = resources.openRawResource(R.raw.blur1_fragment_shader); |
|
| 157 |
public static void createProgram() |
|
| 158 |
{
|
|
| 159 |
final String blurVertex = |
|
| 160 |
|
|
| 161 |
"#version 300 es \n"+ |
|
| 162 |
"precision lowp float; \n"+ |
|
| 163 |
"in vec2 a_Position; \n"+ |
|
| 164 |
"in vec2 a_TexCoord; \n"+ |
|
| 165 |
"out vec2 v_TexCoord; \n"+ |
|
| 166 |
"uniform float u_Depth; \n"+ |
|
| 167 |
|
|
| 168 |
"void main() \n"+ |
|
| 169 |
" { \n"+
|
|
| 170 |
" v_TexCoord = a_TexCoord; \n"+ |
|
| 171 |
" gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+ |
|
| 172 |
"}"; |
|
| 173 |
|
|
| 174 |
final String blurFragment1 = |
|
| 175 |
|
|
| 176 |
"#version 300 es \n"+ |
|
| 177 |
"#define MAX_BLUR "+MAX_HALO+ "\n"+ |
|
| 178 |
"precision lowp float; \n"+ |
|
| 179 |
"in vec2 v_TexCoord; \n"+ |
|
| 180 |
"out vec4 fragColor; \n"+ |
|
| 181 |
"uniform sampler2D u_ColorTexture; \n"+ |
|
| 182 |
"uniform float u_Offsets[MAX_BLUR]; \n"+ |
|
| 183 |
"uniform float u_Weights[MAX_BLUR]; \n"+ |
|
| 184 |
"uniform int u_Radius; \n"+ |
|
| 185 |
|
|
| 186 |
"void main() \n"+ |
|
| 187 |
" { \n"+
|
|
| 188 |
" vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0]; \n"+ |
|
| 189 |
" for (int i=1; i<=u_Radius; i+=1) \n"+ |
|
| 190 |
" { \n"+
|
|
| 191 |
" pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) + \n"+ |
|
| 192 |
" texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+ |
|
| 193 |
" } \n"+ |
|
| 194 |
"fragColor = pixel; \n"+ |
|
| 195 |
"}"; |
|
| 196 |
|
|
| 197 |
final String blurFragment2 = |
|
| 198 |
|
|
| 199 |
"#version 300 es \n"+ |
|
| 200 |
"#define MAX_BLUR "+MAX_HALO+ "\n"+ |
|
| 201 |
"precision lowp float; \n"+ |
|
| 202 |
"in vec2 v_TexCoord; \n"+ |
|
| 203 |
"out vec4 fragColor; \n"+ |
|
| 204 |
"uniform sampler2D u_ColorTexture; \n"+ |
|
| 205 |
"uniform float u_Offsets[MAX_BLUR]; \n"+ |
|
| 206 |
"uniform float u_Weights[MAX_BLUR]; \n"+ |
|
| 207 |
"uniform int u_Radius; \n"+ |
|
| 208 |
|
|
| 209 |
"void main() \n"+ |
|
| 210 |
" { \n"+
|
|
| 211 |
" vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0]; \n"+ |
|
| 212 |
" for (int i=1; i<=u_Radius; i+=1) \n"+ |
|
| 213 |
" { \n"+
|
|
| 214 |
" pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) + \n"+ |
|
| 215 |
" texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+ |
|
| 216 |
" } \n"+ |
|
| 217 |
"fragColor = pixel; \n"+ |
|
| 218 |
"}"; |
|
| 179 | 219 |
|
| 180 | 220 |
try |
| 181 | 221 |
{
|
| 182 |
mBlur1Program = new DistortedProgram(blur1VertexStream,blur1FragmentStream, |
|
| 183 |
GLSL_VERSION, GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, GLSL); |
|
| 222 |
mBlur1Program = new DistortedProgram(blurVertex,blurFragment1); |
|
| 184 | 223 |
} |
| 185 | 224 |
catch(Exception e) |
| 186 | 225 |
{
|
| 187 |
android.util.Log.e("EFFECTS", "exception trying to compile BLUR1 program: "+e.getMessage());
|
|
| 226 |
android.util.Log.e("Effects", "exception trying to compile BLUR1 program: "+e.getMessage());
|
|
| 188 | 227 |
throw new RuntimeException(e.getMessage()); |
| 189 | 228 |
} |
| 190 | 229 |
|
| 191 |
int blur1ProgramH = mBlur1Program.getProgramHandle(); |
|
| 192 |
mRadius1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_Radius"); |
|
| 193 |
mOffsets1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_Offsets"); |
|
| 194 |
mWeights1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_Weights"); |
|
| 195 |
mDepth1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_Depth"); |
|
| 196 |
mColorTexture1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_ColorTexture"); |
|
| 197 |
|
|
| 198 |
final InputStream blur2VertexStream = resources.openRawResource(R.raw.blur_vertex_shader); |
|
| 199 |
final InputStream blur2FragmentStream = resources.openRawResource(R.raw.blur2_fragment_shader); |
|
| 200 |
|
|
| 201 | 230 |
try |
| 202 | 231 |
{
|
| 203 |
mBlur2Program = new DistortedProgram(blur2VertexStream,blur2FragmentStream, |
|
| 204 |
GLSL_VERSION, GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, GLSL); |
|
| 232 |
mBlur2Program = new DistortedProgram(blurVertex,blurFragment2); |
|
| 205 | 233 |
} |
| 206 | 234 |
catch(Exception e) |
| 207 | 235 |
{
|
| 208 |
android.util.Log.e("EFFECTS", "exception trying to compile BLUR2 program: "+e.getMessage());
|
|
| 236 |
android.util.Log.e("Effects", "exception trying to compile BLUR2 program: "+e.getMessage());
|
|
| 209 | 237 |
throw new RuntimeException(e.getMessage()); |
| 210 | 238 |
} |
| 211 |
|
|
| 212 |
int blur2ProgramH = mBlur2Program.getProgramHandle(); |
|
| 213 |
mRadius2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_Radius"); |
|
| 214 |
mOffsets2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_Offsets"); |
|
| 215 |
mWeights2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_Weights"); |
|
| 216 |
mDepth2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_Depth"); |
|
| 217 |
mColorTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_ColorTexture"); |
|
| 218 | 239 |
} |
| 219 | 240 |
|
| 220 | 241 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| src/main/java/org/distorted/library/main/Distorted.java | ||
|---|---|---|
| 124 | 124 |
|
| 125 | 125 |
final Resources resources = context.getResources(); |
| 126 | 126 |
DistortedEffects.createProgram(resources); |
| 127 |
PostprocessEffectBlur.createProgram(resources);
|
|
| 127 |
PostprocessEffectBlur.createProgram(); |
|
| 128 | 128 |
EffectMessageSender.startSending(); |
| 129 | 129 |
|
| 130 | 130 |
mInitialized = true; |
| 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 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| src/main/res/raw/blur1_fragment_shader.glsl | ||
|---|---|---|
| 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 |
precision lowp float; |
|
| 21 |
|
|
| 22 |
#if __VERSION__ != 100 |
|
| 23 |
in vec2 v_TexCoordinate; |
|
| 24 |
out vec4 fragColor; |
|
| 25 |
#define TEXTURE texture |
|
| 26 |
#define FRAG_COLOR fragColor |
|
| 27 |
#else |
|
| 28 |
varying vec2 v_TexCoordinate; |
|
| 29 |
#define TEXTURE texture2D |
|
| 30 |
#define FRAG_COLOR gl_FragColor |
|
| 31 |
#endif |
|
| 32 |
|
|
| 33 |
uniform sampler2D u_ColorTexture; |
|
| 34 |
uniform float u_Offsets[MAX_BLUR]; |
|
| 35 |
uniform float u_Weights[MAX_BLUR]; |
|
| 36 |
uniform int u_Radius; |
|
| 37 |
|
|
| 38 |
////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 39 |
|
|
| 40 |
void main() |
|
| 41 |
{
|
|
| 42 |
vec4 pixel= TEXTURE(u_ColorTexture,v_TexCoordinate) * u_Weights[0]; |
|
| 43 |
|
|
| 44 |
for (int i=1; i<=u_Radius; i+=1) |
|
| 45 |
{
|
|
| 46 |
pixel += ( TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) + |
|
| 47 |
TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i]; |
|
| 48 |
} |
|
| 49 |
|
|
| 50 |
FRAG_COLOR = pixel; |
|
| 51 |
} |
|
| src/main/res/raw/blur2_fragment_shader.glsl | ||
|---|---|---|
| 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 |
precision lowp float; |
|
| 21 |
|
|
| 22 |
#if __VERSION__ != 100 |
|
| 23 |
in vec2 v_TexCoordinate; |
|
| 24 |
out vec4 fragColor; |
|
| 25 |
#define TEXTURE texture |
|
| 26 |
#define FRAG_COLOR fragColor |
|
| 27 |
#else |
|
| 28 |
varying vec2 v_TexCoordinate; |
|
| 29 |
#define TEXTURE texture2D |
|
| 30 |
#define FRAG_COLOR gl_FragColor |
|
| 31 |
#endif |
|
| 32 |
|
|
| 33 |
uniform sampler2D u_ColorTexture; |
|
| 34 |
uniform float u_Offsets[MAX_BLUR]; |
|
| 35 |
uniform float u_Weights[MAX_BLUR]; |
|
| 36 |
uniform int u_Radius; |
|
| 37 |
|
|
| 38 |
////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 39 |
|
|
| 40 |
void main() |
|
| 41 |
{
|
|
| 42 |
vec4 pixel= TEXTURE(u_ColorTexture,v_TexCoordinate) * u_Weights[0]; |
|
| 43 |
|
|
| 44 |
for (int i=1; i<=u_Radius; i+=1) |
|
| 45 |
{
|
|
| 46 |
pixel += ( TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x,v_TexCoordinate.y+u_Offsets[i])) + |
|
| 47 |
TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x,v_TexCoordinate.y-u_Offsets[i])) ) * u_Weights[i]; |
|
| 48 |
} |
|
| 49 |
|
|
| 50 |
FRAG_COLOR = pixel; |
|
| 51 |
} |
|
| src/main/res/raw/blur_vertex_shader.glsl | ||
|---|---|---|
| 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 |
precision lowp float; |
|
| 21 |
|
|
| 22 |
#if __VERSION__ != 100 |
|
| 23 |
in vec2 a_Position; // Per-vertex position. |
|
| 24 |
in vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in. |
|
| 25 |
out vec2 v_TexCoordinate; // |
|
| 26 |
#else |
|
| 27 |
attribute vec2 a_Position; // Per-vertex position. |
|
| 28 |
attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in. |
|
| 29 |
varying vec2 v_TexCoordinate; // |
|
| 30 |
#endif |
|
| 31 |
|
|
| 32 |
uniform float u_Depth; // distance from the near plane to render plane, in clip coords |
|
| 33 |
|
|
| 34 |
////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 35 |
|
|
| 36 |
void main() |
|
| 37 |
{
|
|
| 38 |
v_TexCoordinate = a_TexCoordinate; |
|
| 39 |
gl_Position = vec4(2.0*a_Position,u_Depth,1.0); |
|
| 40 |
} |
|
Also available in: Unified diff
Simplification in PostprocessEffects