Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a41174ec authored by Alex Sakhartchouk's avatar Alex Sakhartchouk
Browse files

Fixing renderscript uniform binding bugs.

Working on custom shaders.

Change-Id: I0d51e879e1c2b46ef5ab696b35162898f4196fc8
parent ba4da86a
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
@@ -17,6 +17,11 @@
package android.renderscript;


import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import android.content.res.Resources;
import android.util.Config;
import android.util.Log;

@@ -95,6 +100,44 @@ public class Program extends BaseObj {
            return this;
        }

        public BaseProgramBuilder setShader(Resources resources, int resourceID) {
            byte[] str;
            int strLength;
            InputStream is = resources.openRawResource(resourceID);
            try {
                try {
                    str = new byte[1024];
                    strLength = 0;
                    while(true) {
                        int bytesLeft = str.length - strLength;
                        if (bytesLeft == 0) {
                            byte[] buf2 = new byte[str.length * 2];
                            System.arraycopy(str, 0, buf2, 0, str.length);
                            str = buf2;
                            bytesLeft = str.length - strLength;
                        }
                        int bytesRead = is.read(str, strLength, bytesLeft);
                        if (bytesRead <= 0) {
                            break;
                        }
                        strLength += bytesRead;
                    }
                } finally {
                    is.close();
                }
            } catch(IOException e) {
                throw new Resources.NotFoundException();
            }

            try {
                mShader = new String(str, 0, strLength, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                Log.e("Renderscript shader creation", "Could not decode shader string");
            }

            return this;
        }

        public void addInput(Element e) throws IllegalStateException {
            // Should check for consistant and non-conflicting names...
            if(mInputCount >= MAX_INPUT) {
+3 −7
Original line number Diff line number Diff line
@@ -107,14 +107,10 @@ public class ProgramVertex extends Program {
        public Allocation mAlloc;

        public MatrixAllocation(RenderScript rs) {
            mModel = new Matrix4f();
            mProjection = new Matrix4f();
            mTexture = new Matrix4f();

            mAlloc = Allocation.createSized(rs, Element.createUser(rs, Element.DataType.FLOAT_32), 48);
            mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
            mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
            mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
            loadModelview(new Matrix4f());
            loadProjection(new Matrix4f());
            loadTexture(new Matrix4f());
        }

        public void destroy() {
+18 −0
Original line number Diff line number Diff line

varying lowp float light0_Diffuse;
varying lowp float light0_Specular;
varying lowp float light1_Diffuse;
varying lowp float light1_Specular;

void main() {
   vec2 t0 = varTex0.xy;
   lowp vec4 col = texture2D(uni_Tex0, t0).rgba;
   /*col = col * (light0_Diffuse * UNI_light0_DiffuseColor + light1_Diffuse * UNI_light1_DiffuseColor);
   col += light0_Specular * UNI_light0_SpecularColor;
   col += light1_Specular * UNI_light1_SpecularColor;*/
   col = col * (light0_Diffuse + light1_Diffuse);
   col += light0_Specular;
   col += light1_Specular;
   gl_FragColor = col;
}
+42 −0
Original line number Diff line number Diff line
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;
*/

// This is where actual shader code begins
void main() {
   vec4 worldPos = UNI_model * ATTRIB_position;
   gl_Position = UNI_MVP * worldPos;

   mat3 model3 = mat3(UNI_model[0].xyz, UNI_model[1].xyz, UNI_model[2].xyz);
   vec3 worldNorm = model3 * ATTRIB_normal;
   vec3 V = normalize(-worldPos.xyz);

   vec3 light0Vec = normalize(UNI_light0_Posision - 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 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);
   light1_Specular = pow(light1Spec, UNI_light1_CosinePower) * UNI_light1_Specular;

   gl_PointSize = 1.0;
   varTex0 = ATTRIB_texture0;
}
+521 KiB (558 KiB)

File changed.

No diff preview for this file type.

Loading