Loading graphics/java/android/renderscript/ProgramFragment.java +92 −14 Original line number Diff line number Diff line Loading @@ -62,9 +62,9 @@ public class ProgramFragment extends Program { } } public static class Builder { public static class Builder extends ShaderBuilder { public static final int MAX_TEXTURE = 2; RenderScript mRS; int mNumTextures; boolean mPointSpriteEnable; boolean mVaryingColorEnable; Loading Loading @@ -101,7 +101,72 @@ public class ProgramFragment extends Program { } Slot[] mSlots; private void buildShaderString() { mShader = "//rs_shader_internal\n"; mShader += "varying lowp vec4 varColor;\n"; mShader += "varying vec4 varTex0;\n"; mShader += "void main() {\n"; if (mVaryingColorEnable) { mShader += " lowp vec4 col = varColor;\n"; } else { mShader += " lowp vec4 col = UNI_Color;\n"; } if (mNumTextures != 0) { if (mPointSpriteEnable) { mShader += " vec2 t0 = gl_PointCoord;\n"; } else { mShader += " vec2 t0 = varTex0.xy;\n"; } } for(int i = 0; i < mNumTextures; i ++) { switch(mSlots[i].env) { case REPLACE: switch (mSlots[i].format) { case ALPHA: mShader += " col.a = texture2D(UNI_Tex0, t0).a;\n"; break; case LUMINANCE_ALPHA: mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n"; break; case RGB: mShader += " col.rgb = texture2D(UNI_Tex0, t0).rgb;\n"; break; case RGBA: mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n"; break; } break; case MODULATE: switch (mSlots[i].format) { case ALPHA: mShader += " col.a *= texture2D(UNI_Tex0, t0).a;\n"; break; case LUMINANCE_ALPHA: mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n"; break; case RGB: mShader += " col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n"; break; case RGBA: mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n"; break; } break; case DECAL: mShader += " col = texture2D(UNI_Tex0, t0);\n"; break; } } mShader += " gl_FragColor = col;\n"; mShader += "}\n"; } public Builder(RenderScript rs) { super(rs); mRS = rs; mSlots = new Slot[MAX_TEXTURE]; mPointSpriteEnable = false; Loading @@ -126,22 +191,35 @@ public class ProgramFragment extends Program { return this; } @Override public ProgramFragment create() { mRS.validate(); int[] tmp = new int[MAX_TEXTURE * 2 + 2]; if (mSlots[0] != null) { tmp[0] = mSlots[0].env.mID; tmp[1] = mSlots[0].format.mID; } if (mSlots[1] != null) { tmp[2] = mSlots[1].env.mID; tmp[3] = mSlots[1].format.mID; } tmp[4] = mPointSpriteEnable ? 1 : 0; tmp[5] = mVaryingColorEnable ? 1 : 0; int id = mRS.nProgramFragmentCreate(tmp); ProgramFragment pf = new ProgramFragment(id, mRS); mNumTextures = 0; for(int i = 0; i < MAX_TEXTURE; i ++) { if(mSlots[i] != null) { mNumTextures ++; } } buildShaderString(); Type constType = null; if (!mVaryingColorEnable) { Element.Builder b = new Element.Builder(mRS); b.add(Element.F32_4(mRS), "Color"); Type.Builder typeBuilder = new Type.Builder(mRS, b.create()); typeBuilder.add(Dimension.X, 1); constType = typeBuilder.create(); addConstant(constType); } setTextureCount(mNumTextures); ProgramFragment pf = super.create(); pf.mTextureCount = MAX_TEXTURE; if (!mVaryingColorEnable) { Allocation constantData = Allocation.createTyped(mRS,constType); float[] data = new float[4]; data[0] = data[1] = data[2] = data[3] = 1.0f; constantData.data(data); pf.bindConstants(constantData, 0); } return pf; } } Loading graphics/java/android/renderscript/ProgramVertex.java +83 −30 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package android.renderscript; import android.graphics.Matrix; import android.util.Config; import android.util.Log; Loading @@ -38,29 +39,6 @@ public class ProgramVertex extends Program { bindConstants(va.mAlloc, 0); } public static class Builder { RenderScript mRS; boolean mTextureMatrixEnable; public Builder(RenderScript rs, Element in, Element out) { mRS = rs; } public Builder(RenderScript rs) { mRS = rs; } public Builder setTextureMatrixEnable(boolean enable) { mTextureMatrixEnable = enable; return this; } public ProgramVertex create() { int id = mRS.nProgramVertexCreate(mTextureMatrixEnable); return new ProgramVertex(id, mRS); } } public static class ShaderBuilder extends BaseProgramBuilder { public ShaderBuilder(RenderScript rs) { super(rs); Loading Loading @@ -93,6 +71,68 @@ public class ProgramVertex extends Program { } } public static class Builder extends ShaderBuilder { boolean mTextureMatrixEnable; public Builder(RenderScript rs, Element in, Element out) { super(rs); } public Builder(RenderScript rs) { super(rs); } public Builder setTextureMatrixEnable(boolean enable) { mTextureMatrixEnable = enable; return this; } static Type getConstantInputType(RenderScript rs) { Element.Builder b = new Element.Builder(rs); b.add(Element.MATRIX4X4(rs), "MV"); b.add(Element.MATRIX4X4(rs), "P"); b.add(Element.MATRIX4X4(rs), "TexMatrix"); b.add(Element.MATRIX4X4(rs), "MVP"); Type.Builder typeBuilder = new Type.Builder(rs, b.create()); typeBuilder.add(Dimension.X, 1); return typeBuilder.create(); } private void buildShaderString() { mShader = "//rs_shader_internal\n"; mShader += "varying vec4 varColor;\n"; mShader += "varying vec4 varTex0;\n"; mShader += "void main() {\n"; mShader += " gl_Position = UNI_MVP * ATTRIB_position;\n"; mShader += " gl_PointSize = 1.0;\n"; mShader += " varColor = ATTRIB_color;\n"; if (mTextureMatrixEnable) { mShader += " varTex0 = UNI_TexMatrix * ATTRIB_texture0;\n"; } else { mShader += " varTex0 = ATTRIB_texture0;\n"; } mShader += "}\n"; } @Override public ProgramVertex create() { buildShaderString(); addConstant(getConstantInputType(mRS)); Element.Builder b = new Element.Builder(mRS); b.add(Element.F32_4(mRS), "position"); b.add(Element.F32_4(mRS), "color"); b.add(Element.F32_3(mRS), "normal"); b.add(Element.F32_4(mRS), "texture0"); addInput(b.create()); return super.create(); } } public static class MatrixAllocation { Loading @@ -105,9 +145,14 @@ public class ProgramVertex extends Program { Matrix4f mTexture; public Allocation mAlloc; private FieldPacker mIOBuffer; public MatrixAllocation(RenderScript rs) { mAlloc = Allocation.createSized(rs, Element.createUser(rs, Element.DataType.FLOAT_32), 48); Type constInputType = ProgramVertex.Builder.getConstantInputType(rs); mAlloc = Allocation.createTyped(rs, constInputType); int bufferSize = constInputType.getElement().getSizeBytes()* constInputType.getElementCount(); mIOBuffer = new FieldPacker(bufferSize); loadModelview(new Matrix4f()); loadProjection(new Matrix4f()); loadTexture(new Matrix4f()); Loading @@ -118,24 +163,32 @@ public class ProgramVertex extends Program { mAlloc = null; } private void addToBuffer(int offset, Matrix4f m) { mIOBuffer.reset(offset); for(int i = 0; i < 16; i ++) { mIOBuffer.addF32(m.mMat[i]); } mAlloc.data(mIOBuffer.getData()); } public void loadModelview(Matrix4f m) { mModel = m; mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat); addToBuffer(MODELVIEW_OFFSET*4, m); } public void loadProjection(Matrix4f m) { mProjection = m; mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat); addToBuffer(PROJECTION_OFFSET*4, m); } public void loadTexture(Matrix4f m) { mTexture = m; mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat); addToBuffer(TEXTURE_OFFSET*4, m); } public void setupOrthoWindow(int w, int h) { mProjection.loadOrtho(0,w, h,0, -1,1); mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); addToBuffer(PROJECTION_OFFSET*4, mProjection); } public void setupOrthoNormalized(int w, int h) { Loading @@ -147,7 +200,7 @@ public class ProgramVertex extends Program { float aspect = ((float)h) / w; mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1); } mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); addToBuffer(PROJECTION_OFFSET*4, mProjection); } public void setupProjectionNormalized(int w, int h) { Loading @@ -173,7 +226,7 @@ public class ProgramVertex extends Program { m1.loadMultiply(m1, m2); mProjection = m1; mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); addToBuffer(PROJECTION_OFFSET*4, mProjection); } } Loading libs/rs/RenderScriptEnv.h +1 −1 Original line number Diff line number Diff line Loading @@ -28,4 +28,4 @@ typedef struct { #define RS_PROGRAM_VERTEX_MODELVIEW_OFFSET 0 #define RS_PROGRAM_VERTEX_PROJECTION_OFFSET 16 #define RS_PROGRAM_VERTEX_TEXTURE_OFFSET 32 #define RS_PROGRAM_VERTEX_MVP_OFFSET 48 libs/rs/java/Samples/res/raw/shaderf.glsl +2 −1 Original line number Diff line number Diff line Loading @@ -3,10 +3,11 @@ varying lowp float light0_Diffuse; varying lowp float light0_Specular; varying lowp float light1_Diffuse; varying lowp float light1_Specular; varying vec2 varTex0; void main() { vec2 t0 = varTex0.xy; lowp vec4 col = texture2D(uni_Tex0, t0).rgba; lowp vec4 col = texture2D(UNI_Tex0, t0).rgba; col.xyz = col.xyz * (light0_Diffuse * UNI_light0_DiffuseColor + light1_Diffuse * UNI_light1_DiffuseColor); col.xyz += light0_Specular * UNI_light0_SpecularColor; col.xyz += light1_Specular * UNI_light1_SpecularColor; Loading libs/rs/java/Samples/res/raw/shaderv.glsl +2 −14 Original line number Diff line number Diff line Loading @@ -2,24 +2,12 @@ varying float light0_Diffuse; varying float light0_Specular; varying float light1_Diffuse; varying float light1_Specular; /* rs_matrix4x4 model; float3 light0_Posision; float light0_Diffuse; float light0_Specular; float light0_CosinePower; float3 light1_Posision; float light1_Diffuse; float light1_Specular; float light1_CosinePower; */ varying vec2 varTex0; // This is where actual shader code begins void main() { vec4 worldPos = UNI_model * ATTRIB_position; gl_Position = UNI_MVP * worldPos; gl_Position = UNI_proj * worldPos; mat3 model3 = mat3(UNI_model[0].xyz, UNI_model[1].xyz, UNI_model[2].xyz); vec3 worldNorm = model3 * ATTRIB_normal; Loading Loading
graphics/java/android/renderscript/ProgramFragment.java +92 −14 Original line number Diff line number Diff line Loading @@ -62,9 +62,9 @@ public class ProgramFragment extends Program { } } public static class Builder { public static class Builder extends ShaderBuilder { public static final int MAX_TEXTURE = 2; RenderScript mRS; int mNumTextures; boolean mPointSpriteEnable; boolean mVaryingColorEnable; Loading Loading @@ -101,7 +101,72 @@ public class ProgramFragment extends Program { } Slot[] mSlots; private void buildShaderString() { mShader = "//rs_shader_internal\n"; mShader += "varying lowp vec4 varColor;\n"; mShader += "varying vec4 varTex0;\n"; mShader += "void main() {\n"; if (mVaryingColorEnable) { mShader += " lowp vec4 col = varColor;\n"; } else { mShader += " lowp vec4 col = UNI_Color;\n"; } if (mNumTextures != 0) { if (mPointSpriteEnable) { mShader += " vec2 t0 = gl_PointCoord;\n"; } else { mShader += " vec2 t0 = varTex0.xy;\n"; } } for(int i = 0; i < mNumTextures; i ++) { switch(mSlots[i].env) { case REPLACE: switch (mSlots[i].format) { case ALPHA: mShader += " col.a = texture2D(UNI_Tex0, t0).a;\n"; break; case LUMINANCE_ALPHA: mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n"; break; case RGB: mShader += " col.rgb = texture2D(UNI_Tex0, t0).rgb;\n"; break; case RGBA: mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n"; break; } break; case MODULATE: switch (mSlots[i].format) { case ALPHA: mShader += " col.a *= texture2D(UNI_Tex0, t0).a;\n"; break; case LUMINANCE_ALPHA: mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n"; break; case RGB: mShader += " col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n"; break; case RGBA: mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n"; break; } break; case DECAL: mShader += " col = texture2D(UNI_Tex0, t0);\n"; break; } } mShader += " gl_FragColor = col;\n"; mShader += "}\n"; } public Builder(RenderScript rs) { super(rs); mRS = rs; mSlots = new Slot[MAX_TEXTURE]; mPointSpriteEnable = false; Loading @@ -126,22 +191,35 @@ public class ProgramFragment extends Program { return this; } @Override public ProgramFragment create() { mRS.validate(); int[] tmp = new int[MAX_TEXTURE * 2 + 2]; if (mSlots[0] != null) { tmp[0] = mSlots[0].env.mID; tmp[1] = mSlots[0].format.mID; } if (mSlots[1] != null) { tmp[2] = mSlots[1].env.mID; tmp[3] = mSlots[1].format.mID; } tmp[4] = mPointSpriteEnable ? 1 : 0; tmp[5] = mVaryingColorEnable ? 1 : 0; int id = mRS.nProgramFragmentCreate(tmp); ProgramFragment pf = new ProgramFragment(id, mRS); mNumTextures = 0; for(int i = 0; i < MAX_TEXTURE; i ++) { if(mSlots[i] != null) { mNumTextures ++; } } buildShaderString(); Type constType = null; if (!mVaryingColorEnable) { Element.Builder b = new Element.Builder(mRS); b.add(Element.F32_4(mRS), "Color"); Type.Builder typeBuilder = new Type.Builder(mRS, b.create()); typeBuilder.add(Dimension.X, 1); constType = typeBuilder.create(); addConstant(constType); } setTextureCount(mNumTextures); ProgramFragment pf = super.create(); pf.mTextureCount = MAX_TEXTURE; if (!mVaryingColorEnable) { Allocation constantData = Allocation.createTyped(mRS,constType); float[] data = new float[4]; data[0] = data[1] = data[2] = data[3] = 1.0f; constantData.data(data); pf.bindConstants(constantData, 0); } return pf; } } Loading
graphics/java/android/renderscript/ProgramVertex.java +83 −30 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package android.renderscript; import android.graphics.Matrix; import android.util.Config; import android.util.Log; Loading @@ -38,29 +39,6 @@ public class ProgramVertex extends Program { bindConstants(va.mAlloc, 0); } public static class Builder { RenderScript mRS; boolean mTextureMatrixEnable; public Builder(RenderScript rs, Element in, Element out) { mRS = rs; } public Builder(RenderScript rs) { mRS = rs; } public Builder setTextureMatrixEnable(boolean enable) { mTextureMatrixEnable = enable; return this; } public ProgramVertex create() { int id = mRS.nProgramVertexCreate(mTextureMatrixEnable); return new ProgramVertex(id, mRS); } } public static class ShaderBuilder extends BaseProgramBuilder { public ShaderBuilder(RenderScript rs) { super(rs); Loading Loading @@ -93,6 +71,68 @@ public class ProgramVertex extends Program { } } public static class Builder extends ShaderBuilder { boolean mTextureMatrixEnable; public Builder(RenderScript rs, Element in, Element out) { super(rs); } public Builder(RenderScript rs) { super(rs); } public Builder setTextureMatrixEnable(boolean enable) { mTextureMatrixEnable = enable; return this; } static Type getConstantInputType(RenderScript rs) { Element.Builder b = new Element.Builder(rs); b.add(Element.MATRIX4X4(rs), "MV"); b.add(Element.MATRIX4X4(rs), "P"); b.add(Element.MATRIX4X4(rs), "TexMatrix"); b.add(Element.MATRIX4X4(rs), "MVP"); Type.Builder typeBuilder = new Type.Builder(rs, b.create()); typeBuilder.add(Dimension.X, 1); return typeBuilder.create(); } private void buildShaderString() { mShader = "//rs_shader_internal\n"; mShader += "varying vec4 varColor;\n"; mShader += "varying vec4 varTex0;\n"; mShader += "void main() {\n"; mShader += " gl_Position = UNI_MVP * ATTRIB_position;\n"; mShader += " gl_PointSize = 1.0;\n"; mShader += " varColor = ATTRIB_color;\n"; if (mTextureMatrixEnable) { mShader += " varTex0 = UNI_TexMatrix * ATTRIB_texture0;\n"; } else { mShader += " varTex0 = ATTRIB_texture0;\n"; } mShader += "}\n"; } @Override public ProgramVertex create() { buildShaderString(); addConstant(getConstantInputType(mRS)); Element.Builder b = new Element.Builder(mRS); b.add(Element.F32_4(mRS), "position"); b.add(Element.F32_4(mRS), "color"); b.add(Element.F32_3(mRS), "normal"); b.add(Element.F32_4(mRS), "texture0"); addInput(b.create()); return super.create(); } } public static class MatrixAllocation { Loading @@ -105,9 +145,14 @@ public class ProgramVertex extends Program { Matrix4f mTexture; public Allocation mAlloc; private FieldPacker mIOBuffer; public MatrixAllocation(RenderScript rs) { mAlloc = Allocation.createSized(rs, Element.createUser(rs, Element.DataType.FLOAT_32), 48); Type constInputType = ProgramVertex.Builder.getConstantInputType(rs); mAlloc = Allocation.createTyped(rs, constInputType); int bufferSize = constInputType.getElement().getSizeBytes()* constInputType.getElementCount(); mIOBuffer = new FieldPacker(bufferSize); loadModelview(new Matrix4f()); loadProjection(new Matrix4f()); loadTexture(new Matrix4f()); Loading @@ -118,24 +163,32 @@ public class ProgramVertex extends Program { mAlloc = null; } private void addToBuffer(int offset, Matrix4f m) { mIOBuffer.reset(offset); for(int i = 0; i < 16; i ++) { mIOBuffer.addF32(m.mMat[i]); } mAlloc.data(mIOBuffer.getData()); } public void loadModelview(Matrix4f m) { mModel = m; mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat); addToBuffer(MODELVIEW_OFFSET*4, m); } public void loadProjection(Matrix4f m) { mProjection = m; mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat); addToBuffer(PROJECTION_OFFSET*4, m); } public void loadTexture(Matrix4f m) { mTexture = m; mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat); addToBuffer(TEXTURE_OFFSET*4, m); } public void setupOrthoWindow(int w, int h) { mProjection.loadOrtho(0,w, h,0, -1,1); mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); addToBuffer(PROJECTION_OFFSET*4, mProjection); } public void setupOrthoNormalized(int w, int h) { Loading @@ -147,7 +200,7 @@ public class ProgramVertex extends Program { float aspect = ((float)h) / w; mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1); } mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); addToBuffer(PROJECTION_OFFSET*4, mProjection); } public void setupProjectionNormalized(int w, int h) { Loading @@ -173,7 +226,7 @@ public class ProgramVertex extends Program { m1.loadMultiply(m1, m2); mProjection = m1; mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); addToBuffer(PROJECTION_OFFSET*4, mProjection); } } Loading
libs/rs/RenderScriptEnv.h +1 −1 Original line number Diff line number Diff line Loading @@ -28,4 +28,4 @@ typedef struct { #define RS_PROGRAM_VERTEX_MODELVIEW_OFFSET 0 #define RS_PROGRAM_VERTEX_PROJECTION_OFFSET 16 #define RS_PROGRAM_VERTEX_TEXTURE_OFFSET 32 #define RS_PROGRAM_VERTEX_MVP_OFFSET 48
libs/rs/java/Samples/res/raw/shaderf.glsl +2 −1 Original line number Diff line number Diff line Loading @@ -3,10 +3,11 @@ varying lowp float light0_Diffuse; varying lowp float light0_Specular; varying lowp float light1_Diffuse; varying lowp float light1_Specular; varying vec2 varTex0; void main() { vec2 t0 = varTex0.xy; lowp vec4 col = texture2D(uni_Tex0, t0).rgba; lowp vec4 col = texture2D(UNI_Tex0, t0).rgba; col.xyz = col.xyz * (light0_Diffuse * UNI_light0_DiffuseColor + light1_Diffuse * UNI_light1_DiffuseColor); col.xyz += light0_Specular * UNI_light0_SpecularColor; col.xyz += light1_Specular * UNI_light1_SpecularColor; Loading
libs/rs/java/Samples/res/raw/shaderv.glsl +2 −14 Original line number Diff line number Diff line Loading @@ -2,24 +2,12 @@ varying float light0_Diffuse; varying float light0_Specular; varying float light1_Diffuse; varying float light1_Specular; /* rs_matrix4x4 model; float3 light0_Posision; float light0_Diffuse; float light0_Specular; float light0_CosinePower; float3 light1_Posision; float light1_Diffuse; float light1_Specular; float light1_CosinePower; */ varying vec2 varTex0; // This is where actual shader code begins void main() { vec4 worldPos = UNI_model * ATTRIB_position; gl_Position = UNI_MVP * worldPos; gl_Position = UNI_proj * worldPos; mat3 model3 = mat3(UNI_model[0].xyz, UNI_model[1].xyz, UNI_model[2].xyz); vec3 worldNorm = model3 * ATTRIB_normal; Loading