FragmentShader.java revision 4bda82de0bca754f3ce387e9968170c5122241a9
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.scenegraph; 18 19import java.lang.Math; 20import java.util.ArrayList; 21 22import android.content.res.Resources; 23import android.renderscript.*; 24import android.renderscript.ProgramFragment.Builder; 25import android.util.Log; 26 27/** 28 * @hide 29 */ 30public class FragmentShader extends Shader { 31 ProgramFragment mProgram; 32 ScriptField_FragmentShader_s mField; 33 34 public static class Builder { 35 36 FragmentShader mShader; 37 ProgramFragment.Builder mBuilder; 38 39 public Builder(RenderScriptGL rs) { 40 mShader = new FragmentShader(); 41 mBuilder = new ProgramFragment.Builder(rs); 42 } 43 44 public Builder setShader(Resources resources, int resourceID) { 45 mBuilder.setShader(resources, resourceID); 46 return this; 47 } 48 49 public Builder setObjectConst(Type type) { 50 mShader.mPerObjConstants = type; 51 return this; 52 } 53 54 public Builder setShaderConst(Type type) { 55 mShader.mPerShaderConstants = type; 56 return this; 57 } 58 59 public Builder addShaderTexture(Program.TextureType texType, String name) { 60 mShader.mShaderTextureNames.add(name); 61 mShader.mShaderTextureTypes.add(texType); 62 return this; 63 } 64 65 public Builder addTexture(Program.TextureType texType, String name) { 66 mShader.mTextureNames.add(name); 67 mShader.mTextureTypes.add(texType); 68 return this; 69 } 70 71 public FragmentShader create() { 72 if (mShader.mPerShaderConstants != null) { 73 mBuilder.addConstant(mShader.mPerShaderConstants); 74 } 75 if (mShader.mPerObjConstants != null) { 76 mBuilder.addConstant(mShader.mPerObjConstants); 77 } 78 for (int i = 0; i < mShader.mTextureTypes.size(); i ++) { 79 mBuilder.addTexture(mShader.mTextureTypes.get(i)); 80 } 81 for (int i = 0; i < mShader.mShaderTextureTypes.size(); i ++) { 82 mBuilder.addTexture(mShader.mShaderTextureTypes.get(i)); 83 } 84 85 mShader.mProgram = mBuilder.create(); 86 return mShader; 87 } 88 } 89 90 public ProgramFragment getProgram() { 91 return mProgram; 92 } 93 94 public void updateTextures(RenderScriptGL rs, Resources res) { 95 int shaderTextureStart = mTextureTypes.size(); 96 for (int i = 0; i < mShaderTextureNames.size(); i ++) { 97 ShaderParam sp = mSourceParams.get(mShaderTextureNames.get(i)); 98 if (sp != null && sp instanceof TextureParam) { 99 TextureParam p = (TextureParam)sp; 100 mProgram.bindTexture(p.getTexture().getRsData(rs, res), shaderTextureStart + i); 101 } 102 } 103 } 104 105 public ScriptField_FragmentShader_s getRSData(RenderScriptGL rs, Resources res) { 106 if (mField != null) { 107 return mField; 108 } 109 110 ScriptField_FragmentShader_s.Item item = new ScriptField_FragmentShader_s.Item(); 111 item.program = mProgram; 112 113 linkConstants(rs, res); 114 if (mPerShaderConstants != null) { 115 item.shaderConst = mConstantBuffer; 116 item.shaderConstParams = mConstantBufferParams; 117 mProgram.bindConstants(item.shaderConst, 0); 118 } 119 120 item.objectConstIndex = -1; 121 if (mPerObjConstants != null) { 122 item.objectConstIndex = mPerShaderConstants != null ? 1 : 0; 123 } 124 125 mField = new ScriptField_FragmentShader_s(rs, 1); 126 mField.set(item, 0, true); 127 return mField; 128 } 129} 130