From 9d71e2180062931416092f26276a07e55b318f62 Mon Sep 17 00:00:00 2001 From: Alex Sakhartchouk Date: Mon, 8 Nov 2010 15:10:52 -0800 Subject: Moving attrib creation to Mesh. Adding arrays as shader inputs. Removing fixed size arrays. Change-Id: I0213e403a2f1283dd43f21bea770aeb059561903 --- libs/rs/java/Samples/res/raw/shaderarrayf.glsl | 16 +++++ libs/rs/java/Samples/res/raw/shaderarrayv.glsl | 32 +++++++++ libs/rs/java/Samples/res/raw/shaderf.glsl | 6 +- libs/rs/java/Samples/res/raw/shaderv.glsl | 6 +- .../src/com/android/samples/RsRenderStatesRS.java | 30 +++++++- .../src/com/android/samples/rsrenderstates.rs | 83 ++++++++++++++++++---- .../Samples/src/com/android/samples/shader_def.rsh | 27 +++++-- 7 files changed, 174 insertions(+), 26 deletions(-) create mode 100644 libs/rs/java/Samples/res/raw/shaderarrayf.glsl create mode 100644 libs/rs/java/Samples/res/raw/shaderarrayv.glsl (limited to 'libs/rs/java/Samples') diff --git a/libs/rs/java/Samples/res/raw/shaderarrayf.glsl b/libs/rs/java/Samples/res/raw/shaderarrayf.glsl new file mode 100644 index 0000000..238ecad --- /dev/null +++ b/libs/rs/java/Samples/res/raw/shaderarrayf.glsl @@ -0,0 +1,16 @@ + +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; + col.xyz = col.xyz * (light0_Diffuse * UNI_light_DiffuseColor[0].xyz + light1_Diffuse * UNI_light_DiffuseColor[1].xyz); + col.xyz += light0_Specular * UNI_light_SpecularColor[0].xyz; + col.xyz += light1_Specular * UNI_light_SpecularColor[1].xyz; + gl_FragColor = col; +} + diff --git a/libs/rs/java/Samples/res/raw/shaderarrayv.glsl b/libs/rs/java/Samples/res/raw/shaderarrayv.glsl new file mode 100644 index 0000000..7a1310a --- /dev/null +++ b/libs/rs/java/Samples/res/raw/shaderarrayv.glsl @@ -0,0 +1,32 @@ +varying float light0_Diffuse; +varying float light0_Specular; +varying float light1_Diffuse; +varying float light1_Specular; +varying vec2 varTex0; + +// This is where actual shader code begins +void main() { + vec4 worldPos = UNI_model[0] * ATTRIB_position; + worldPos = UNI_model[1] * worldPos; + gl_Position = UNI_proj * worldPos; + + mat4 model0 = UNI_model[0]; + mat3 model3 = mat3(model0[0].xyz, model0[1].xyz, model0[2].xyz); + vec3 worldNorm = model3 * ATTRIB_normal; + vec3 V = normalize(-worldPos.xyz); + + vec3 light0Vec = normalize(UNI_light_Posision[0].xyz - worldPos.xyz); + vec3 light0R = -reflect(light0Vec, worldNorm); + light0_Diffuse = clamp(dot(worldNorm, light0Vec), 0.0, 1.0) * UNI_light_Diffuse[0]; + float light0Spec = clamp(dot(light0R, V), 0.001, 1.0); + light0_Specular = pow(light0Spec, UNI_light_CosinePower[0]) * UNI_light_Specular[0]; + + vec3 light1Vec = normalize(UNI_light_Posision[1].xyz - worldPos.xyz); + vec3 light1R = reflect(light1Vec, worldNorm); + light1_Diffuse = clamp(dot(worldNorm, light1Vec), 0.0, 1.0) * UNI_light_Diffuse[1]; + float light1Spec = clamp(dot(light1R, V), 0.001, 1.0); + light1_Specular = pow(light1Spec, UNI_light_CosinePower[1]) * UNI_light_Specular[1]; + + gl_PointSize = 1.0; + varTex0 = ATTRIB_texture0; +} diff --git a/libs/rs/java/Samples/res/raw/shaderf.glsl b/libs/rs/java/Samples/res/raw/shaderf.glsl index fcbe7ee..d56e203 100644 --- a/libs/rs/java/Samples/res/raw/shaderf.glsl +++ b/libs/rs/java/Samples/res/raw/shaderf.glsl @@ -8,9 +8,9 @@ varying vec2 varTex0; void main() { vec2 t0 = varTex0.xy; 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; + col.xyz = col.xyz * (light0_Diffuse * UNI_light0_DiffuseColor.xyz + light1_Diffuse * UNI_light1_DiffuseColor.xyz); + col.xyz += light0_Specular * UNI_light0_SpecularColor.xyz; + col.xyz += light1_Specular * UNI_light1_SpecularColor.xyz; gl_FragColor = col; } diff --git a/libs/rs/java/Samples/res/raw/shaderv.glsl b/libs/rs/java/Samples/res/raw/shaderv.glsl index 867589c..f7d01de 100644 --- a/libs/rs/java/Samples/res/raw/shaderv.glsl +++ b/libs/rs/java/Samples/res/raw/shaderv.glsl @@ -13,13 +13,13 @@ void main() { vec3 worldNorm = model3 * ATTRIB_normal; vec3 V = normalize(-worldPos.xyz); - vec3 light0Vec = normalize(UNI_light0_Posision - worldPos.xyz); - vec3 light0R = reflect(light0Vec, worldNorm); + vec3 light0Vec = normalize(UNI_light0_Posision.xyz - worldPos.xyz); + vec3 light0R = -reflect(light0Vec, worldNorm); light0_Diffuse = clamp(dot(worldNorm, light0Vec), 0.0, 1.0) * UNI_light0_Diffuse; float light0Spec = clamp(dot(light0R, V), 0.001, 1.0); light0_Specular = pow(light0Spec, UNI_light0_CosinePower) * UNI_light0_Specular; - vec3 light1Vec = normalize(UNI_light1_Posision - worldPos.xyz); + vec3 light1Vec = normalize(UNI_light1_Posision.xyz - worldPos.xyz); vec3 light1R = reflect(light1Vec, worldNorm); light1_Diffuse = clamp(dot(worldNorm, light1Vec), 0.0, 1.0) * UNI_light1_Diffuse; float light1Spec = clamp(dot(light1R, V), 0.001, 1.0); diff --git a/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java b/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java index 85c2557..dd2daa7 100644 --- a/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java +++ b/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java @@ -43,7 +43,7 @@ public class RsRenderStatesRS { mOptionsARGB.inScaled = false; mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888; mMode = 0; - mMaxModes = 9; + mMaxModes = 0; initRS(); } @@ -73,7 +73,12 @@ public class RsRenderStatesRS { private ProgramFragment mProgFragmentCustom; private ProgramFragment mProgFragmentMultitex; private ScriptField_VertexShaderConstants_s mVSConst; + private ScriptField_VertexShaderConstants2_s mVSConst2; private ScriptField_FragentShaderConstants_s mFSConst; + private ScriptField_FragentShaderConstants2_s mFSConst2; + + private ProgramVertex mProgVertexCustom2; + private ProgramFragment mProgFragmentCustom2; private ProgramRaster mCullBack; private ProgramRaster mCullFront; @@ -189,10 +194,14 @@ public class RsRenderStatesRS { private void initCustomShaders() { mVSConst = new ScriptField_VertexShaderConstants_s(mRS, 1); + mVSConst2 = new ScriptField_VertexShaderConstants2_s(mRS, 1); mFSConst = new ScriptField_FragentShaderConstants_s(mRS, 1); + mFSConst2 = new ScriptField_FragentShaderConstants2_s(mRS, 1); mScript.bind_gVSConstants(mVSConst); + mScript.bind_gVSConstants2(mVSConst2); mScript.bind_gFSConstants(mFSConst); + mScript.bind_gFSConstants2(mFSConst2); // Initialize the shader builder ProgramVertex.ShaderBuilder pvbCustom = new ProgramVertex.ShaderBuilder(mRS); @@ -217,6 +226,20 @@ public class RsRenderStatesRS { // Bind the source of constant data mProgFragmentCustom.bindConstants(mFSConst.getAllocation(), 0); + pvbCustom = new ProgramVertex.ShaderBuilder(mRS); + pvbCustom.setShader(mRes, R.raw.shaderarrayv); + pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS)); + pvbCustom.addConstant(mVSConst2.getAllocation().getType()); + mProgVertexCustom2 = pvbCustom.create(); + mProgVertexCustom2.bindConstants(mVSConst2.getAllocation(), 0); + + pfbCustom = new ProgramFragment.ShaderBuilder(mRS); + pfbCustom.setShader(mRes, R.raw.shaderarrayf); + pfbCustom.setTextureCount(1); + pfbCustom.addConstant(mFSConst2.getAllocation().getType()); + mProgFragmentCustom2 = pfbCustom.create(); + mProgFragmentCustom2.bindConstants(mFSConst2.getAllocation(), 0); + pfbCustom = new ProgramFragment.ShaderBuilder(mRS); pfbCustom.setShader(mRes, R.raw.multitexf); pfbCustom.setTextureCount(3); @@ -225,6 +248,9 @@ public class RsRenderStatesRS { mScript.set_gProgVertexCustom(mProgVertexCustom); mScript.set_gProgFragmentCustom(mProgFragmentCustom); mScript.set_gProgFragmentMultitex(mProgFragmentMultitex); + + mScript.set_gProgVertexCustom2(mProgVertexCustom2); + mScript.set_gProgFragmentCustom2(mProgFragmentCustom2); } private Allocation loadTextureRGB(int id) { @@ -334,6 +360,8 @@ public class RsRenderStatesRS { mScript = new ScriptC_rsrenderstates(mRS, mRes, R.raw.rsrenderstates); + mMaxModes = mScript.get_gMaxModes(); + initSamplers(); initProgramStore(); initProgramFragment(); diff --git a/libs/rs/java/Samples/src/com/android/samples/rsrenderstates.rs b/libs/rs/java/Samples/src/com/android/samples/rsrenderstates.rs index 8be35f8..4f8eada 100644 --- a/libs/rs/java/Samples/src/com/android/samples/rsrenderstates.rs +++ b/libs/rs/java/Samples/src/com/android/samples/rsrenderstates.rs @@ -19,6 +19,8 @@ #include "rs_graphics.rsh" #include "shader_def.rsh" +const int gMaxModes = 10; + rs_program_vertex gProgVertex; rs_program_fragment gProgFragmentColor; rs_program_fragment gProgFragmentTexture; @@ -59,12 +61,16 @@ rs_program_raster gCullNone; // Custom vertex shader compunents VertexShaderConstants *gVSConstants; +VertexShaderConstants2 *gVSConstants2; FragentShaderConstants *gFSConstants; +FragentShaderConstants2 *gFSConstants2; // Export these out to easily set the inputs to shader VertexShaderInputs *gVSInputs; // Custom shaders we use for lighting rs_program_vertex gProgVertexCustom; rs_program_fragment gProgFragmentCustom; +rs_program_vertex gProgVertexCustom2; +rs_program_fragment gProgFragmentCustom2; rs_program_fragment gProgFragmentMultitex; float gDt = 0; @@ -367,10 +373,10 @@ float gLight1Rotation = 0; void setupCustomShaderLights() { float4 light0Pos = {-5.0f, 5.0f, -10.0f, 1.0f}; float4 light1Pos = {2.0f, 5.0f, 15.0f, 1.0f}; - float3 light0DiffCol = {0.9f, 0.7f, 0.7f}; - float3 light0SpecCol = {0.9f, 0.6f, 0.6f}; - float3 light1DiffCol = {0.5f, 0.5f, 0.9f}; - float3 light1SpecCol = {0.5f, 0.5f, 0.9f}; + float4 light0DiffCol = {0.9f, 0.7f, 0.7f, 1.0f}; + float4 light0SpecCol = {0.9f, 0.6f, 0.6f, 1.0f}; + float4 light1DiffCol = {0.5f, 0.5f, 0.9f, 1.0f}; + float4 light1SpecCol = {0.5f, 0.5f, 0.9f, 1.0f}; gLight0Rotation += 50.0f * gDt; if(gLight0Rotation > 360.0f) { @@ -389,21 +395,27 @@ void setupCustomShaderLights() { light1Pos = rsMatrixMultiply(&l1Mat, light1Pos); // Set light 0 properties - gVSConstants->light0_Posision.x = light0Pos.x; - gVSConstants->light0_Posision.y = light0Pos.y; - gVSConstants->light0_Posision.z = light0Pos.z; + gVSConstants->light0_Posision = light0Pos; gVSConstants->light0_Diffuse = 1.0f; gVSConstants->light0_Specular = 0.5f; - gVSConstants->light0_CosinePower = 40.0f; + gVSConstants->light0_CosinePower = 10.0f; // Set light 1 properties - gVSConstants->light1_Posision.x = light1Pos.x; - gVSConstants->light1_Posision.y = light1Pos.y; - gVSConstants->light1_Posision.z = light1Pos.z; + gVSConstants->light1_Posision = light1Pos; gVSConstants->light1_Diffuse = 1.0f; gVSConstants->light1_Specular = 0.7f; - gVSConstants->light1_CosinePower = 50.0f; + gVSConstants->light1_CosinePower = 25.0f; rsAllocationMarkDirty(rsGetAllocation(gVSConstants)); + gVSConstants2->light_Posision[0] = light0Pos; + gVSConstants2->light_Diffuse[0] = 1.0f; + gVSConstants2->light_Specular[0] = 0.5f; + gVSConstants2->light_CosinePower[0] = 10.0f; + gVSConstants2->light_Posision[1] = light1Pos; + gVSConstants2->light_Diffuse[1] = 1.0f; + gVSConstants2->light_Specular[1] = 0.7f; + gVSConstants2->light_CosinePower[1] = 25.0f; + rsAllocationMarkDirty(rsGetAllocation(gVSConstants2)); + // Update fragmetn shader constants // Set light 0 colors gFSConstants->light0_DiffuseColor = light0DiffCol; @@ -412,6 +424,13 @@ void setupCustomShaderLights() { gFSConstants->light1_DiffuseColor = light1DiffCol; gFSConstants->light1_SpecularColor = light1SpecCol; rsAllocationMarkDirty(rsGetAllocation(gFSConstants)); + + gFSConstants2->light_DiffuseColor[0] = light0DiffCol; + gFSConstants2->light_SpecularColor[0] = light0SpecCol; + // Set light 1 colors + gFSConstants2->light_DiffuseColor[1] = light1DiffCol; + gFSConstants2->light_SpecularColor[1] = light1SpecCol; + rsAllocationMarkDirty(rsGetAllocation(gFSConstants2)); } void displayCustomShaderSamples() { @@ -450,6 +469,43 @@ void displayCustomShaderSamples() { rsgDrawText("Custom shader sample", 10, rsgGetHeight() - 10); } +void displayCustomShaderSamples2() { + + // Update vertex shader constants + // Load model matrix + // Aplly a rotation to our mesh + gTorusRotation += 50.0f * gDt; + if(gTorusRotation > 360.0f) { + gTorusRotation -= 360.0f; + } + + // Position our model on the screen + rsMatrixLoadTranslate(&gVSConstants2->model[1], 0.0f, 0.0f, -10.0f); + rsMatrixLoadIdentity(&gVSConstants2->model[0]); + rsMatrixRotate(&gVSConstants2->model[0], gTorusRotation, 1.0f, 0.0f, 0.0f); + rsMatrixRotate(&gVSConstants2->model[0], gTorusRotation, 0.0f, 0.0f, 1.0f); + // Setup the projectioni matrix + float aspect = (float)rsgGetWidth() / (float)rsgGetHeight(); + rsMatrixLoadPerspective(&gVSConstants2->proj, 30.0f, aspect, 0.1f, 100.0f); + setupCustomShaderLights(); + + rsgBindProgramVertex(gProgVertexCustom2); + + // Fragment shader with texture + rsgBindProgramStore(gProgStoreBlendNoneDepth); + rsgBindProgramFragment(gProgFragmentCustom2); + rsgBindSampler(gProgFragmentCustom2, 0, gLinearClamp); + rsgBindTexture(gProgFragmentCustom2, 0, gTexTorus); + + // Use back face culling + rsgBindProgramRaster(gCullBack); + rsgDrawMesh(gTorusMesh); + + rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f); + rsgBindFont(gFontMono); + rsgDrawText("Custom shader sample with array uniforms", 10, rsgGetHeight() - 10); +} + void displayMultitextureSample() { bindProgramVertexOrtho(); rs_matrix4x4 matrix; @@ -577,6 +633,9 @@ int root(int launchID) { case 8: displayAnisoSample(); break; + case 9: + displayCustomShaderSamples2(); + break; } return 10; diff --git a/libs/rs/java/Samples/src/com/android/samples/shader_def.rsh b/libs/rs/java/Samples/src/com/android/samples/shader_def.rsh index e3f6206..3f51785 100644 --- a/libs/rs/java/Samples/src/com/android/samples/shader_def.rsh +++ b/libs/rs/java/Samples/src/com/android/samples/shader_def.rsh @@ -19,26 +19,39 @@ typedef struct VertexShaderConstants_s { rs_matrix4x4 model; rs_matrix4x4 proj; - float3 light0_Posision; + float4 light0_Posision; float light0_Diffuse; float light0_Specular; float light0_CosinePower; - float3 light1_Posision; + float4 light1_Posision; float light1_Diffuse; float light1_Specular; float light1_CosinePower; } VertexShaderConstants; -typedef struct FragentShaderConstants_s { - float3 light0_DiffuseColor; - float3 light0_SpecularColor; +typedef struct VertexShaderConstants2_s { + rs_matrix4x4 model[2]; + rs_matrix4x4 proj; + float4 light_Posision[2]; + float light_Diffuse[2]; + float light_Specular[2]; + float light_CosinePower[2]; +} VertexShaderConstants2; - float3 light1_DiffuseColor; - float3 light1_SpecularColor; +typedef struct FragentShaderConstants_s { + float4 light0_DiffuseColor; + float4 light0_SpecularColor; + float4 light1_DiffuseColor; + float4 light1_SpecularColor; } FragentShaderConstants; +typedef struct FragentShaderConstants2_s { + float4 light_DiffuseColor[2]; + float4 light_SpecularColor[2]; +} FragentShaderConstants2; + typedef struct VertexShaderInputs_s { float4 position; float3 normal; -- cgit v1.1