46 /** 47 */ 48 public class JSWBackend extends TreeScanner { 49 50 private final JSLParser parser; 51 private final String body; 52 53 public JSWBackend(JSLParser parser, ProgramUnit program) { 54 // TODO: will be removed once we clean up static usage 55 resetStatics(); 56 57 this.parser = parser; 58 59 JSWTreeScanner scanner = new JSWTreeScanner(); 60 scanner.scan(program); 61 this.body = scanner.getResult(); 62 } 63 64 public final String getGenCode(String effectName, 65 String peerName, 66 String interfaceName) 67 { 68 Map<String, Variable> vars = parser.getSymbolTable().getGlobalVariables(); 69 StringBuilder interfaceDecl = new StringBuilder(); 70 StringBuilder constants = new StringBuilder(); 71 StringBuilder samplers = new StringBuilder(); 72 StringBuilder cleanup = new StringBuilder(); 73 StringBuilder srcRects = new StringBuilder(); 74 StringBuilder posDecls = new StringBuilder(); 75 StringBuilder pixInitY = new StringBuilder(); 76 StringBuilder pixInitX = new StringBuilder(); 77 StringBuilder posIncrY = new StringBuilder(); 78 StringBuilder posInitY = new StringBuilder(); 79 StringBuilder posIncrX = new StringBuilder(); 80 StringBuilder posInitX = new StringBuilder(); 81 82 // TODO: only need to declare these if pixcoord is referenced 83 // somewhere in the program... 84 pixInitY.append("float pixcoord_y = (float)dy;\n"); 85 pixInitX.append("float pixcoord_x = (float)dx;\n"); 86 87 for (Variable v : vars.values()) { 88 if (v.getQualifier() == Qualifier.CONST && v.getConstValue() == null) { 192 // shaders that declare LSAMPLERs would require mapped 193 // inputs and so far that is only Perspective and 194 // Displacement, both of which override getTC() and return 195 // only 4 values. 196 srcRects.append("getTextureCoordinates(" + i + ", src" + i + "Rect,\n"); 197 srcRects.append(" src" + i + "InputBounds.x, src" + i + "InputBounds.y,\n"); 198 srcRects.append(" src" + i + "w, src" + i + "h,\n"); 199 srcRects.append(" dstBounds, src" + i + "Transform);\n"); 200 } 201 202 posDecls.append("float inc" + i + "_x = (src" + i + "Rect[2] - src" + i + "Rect[0]) / dstw;\n"); 203 posDecls.append("float inc" + i + "_y = (src" + i + "Rect[3] - src" + i + "Rect[1]) / dsth;\n"); 204 205 posInitY.append("float pos" + i + "_y = src" + i + "Rect[1] + inc" + i + "_y*0.5f;\n"); 206 posInitX.append("float pos" + i + "_x = src" + i + "Rect[0] + inc" + i + "_x*0.5f;\n"); 207 posIncrX.append("pos" + i + "_x += inc" + i + "_x;\n"); 208 posIncrY.append("pos" + i + "_y += inc" + i + "_y;\n"); 209 } 210 } 211 212 if (interfaceName != null) { 213 interfaceDecl.append("implements "+interfaceName); 214 } 215 216 Reader template = new InputStreamReader(getClass().getResourceAsStream("JSWGlue.stg")); 217 StringTemplateGroup group = new StringTemplateGroup(template, DefaultTemplateLexer.class); 218 StringTemplate glue = group.getInstanceOf("glue"); 219 glue.setAttribute("effectName", effectName); 220 glue.setAttribute("peerName", peerName); 221 glue.setAttribute("interfaceDecl", interfaceDecl.toString()); 222 glue.setAttribute("usercode", usercode.toString()); 223 glue.setAttribute("samplers", samplers.toString()); 224 glue.setAttribute("cleanup", cleanup.toString()); 225 glue.setAttribute("srcRects", srcRects.toString()); 226 glue.setAttribute("constants", constants.toString()); 227 glue.setAttribute("posDecls", posDecls.toString()); 228 glue.setAttribute("pixInitY", pixInitY.toString()); 229 glue.setAttribute("pixInitX", pixInitX.toString()); 230 glue.setAttribute("posIncrY", posIncrY.toString()); 231 glue.setAttribute("posInitY", posInitY.toString()); 232 glue.setAttribute("posIncrX", posIncrX.toString()); 233 glue.setAttribute("posInitX", posInitX.toString()); 234 glue.setAttribute("body", body); 235 return glue.toString(); 236 } 237 238 // TODO: need better mechanism for querying fields 239 private static char[] fields = {'x', 'y', 'z', 'w'}; 240 public static String getSuffix(int i) { | 46 /** 47 */ 48 public class JSWBackend extends TreeScanner { 49 50 private final JSLParser parser; 51 private final String body; 52 53 public JSWBackend(JSLParser parser, ProgramUnit program) { 54 // TODO: will be removed once we clean up static usage 55 resetStatics(); 56 57 this.parser = parser; 58 59 JSWTreeScanner scanner = new JSWTreeScanner(); 60 scanner.scan(program); 61 this.body = scanner.getResult(); 62 } 63 64 public final String getGenCode(String effectName, 65 String peerName, 66 String genericsName, 67 String interfaceName) 68 { 69 Map<String, Variable> vars = parser.getSymbolTable().getGlobalVariables(); 70 StringBuilder genericsDecl = new StringBuilder(); 71 StringBuilder interfaceDecl = new StringBuilder(); 72 StringBuilder constants = new StringBuilder(); 73 StringBuilder samplers = new StringBuilder(); 74 StringBuilder cleanup = new StringBuilder(); 75 StringBuilder srcRects = new StringBuilder(); 76 StringBuilder posDecls = new StringBuilder(); 77 StringBuilder pixInitY = new StringBuilder(); 78 StringBuilder pixInitX = new StringBuilder(); 79 StringBuilder posIncrY = new StringBuilder(); 80 StringBuilder posInitY = new StringBuilder(); 81 StringBuilder posIncrX = new StringBuilder(); 82 StringBuilder posInitX = new StringBuilder(); 83 84 // TODO: only need to declare these if pixcoord is referenced 85 // somewhere in the program... 86 pixInitY.append("float pixcoord_y = (float)dy;\n"); 87 pixInitX.append("float pixcoord_x = (float)dx;\n"); 88 89 for (Variable v : vars.values()) { 90 if (v.getQualifier() == Qualifier.CONST && v.getConstValue() == null) { 194 // shaders that declare LSAMPLERs would require mapped 195 // inputs and so far that is only Perspective and 196 // Displacement, both of which override getTC() and return 197 // only 4 values. 198 srcRects.append("getTextureCoordinates(" + i + ", src" + i + "Rect,\n"); 199 srcRects.append(" src" + i + "InputBounds.x, src" + i + "InputBounds.y,\n"); 200 srcRects.append(" src" + i + "w, src" + i + "h,\n"); 201 srcRects.append(" dstBounds, src" + i + "Transform);\n"); 202 } 203 204 posDecls.append("float inc" + i + "_x = (src" + i + "Rect[2] - src" + i + "Rect[0]) / dstw;\n"); 205 posDecls.append("float inc" + i + "_y = (src" + i + "Rect[3] - src" + i + "Rect[1]) / dsth;\n"); 206 207 posInitY.append("float pos" + i + "_y = src" + i + "Rect[1] + inc" + i + "_y*0.5f;\n"); 208 posInitX.append("float pos" + i + "_x = src" + i + "Rect[0] + inc" + i + "_x*0.5f;\n"); 209 posIncrX.append("pos" + i + "_x += inc" + i + "_x;\n"); 210 posIncrY.append("pos" + i + "_y += inc" + i + "_y;\n"); 211 } 212 } 213 214 if (genericsName != null) { 215 genericsDecl.append("<"+genericsName+">"); 216 } 217 218 if (interfaceName != null) { 219 interfaceDecl.append("implements "+interfaceName); 220 } 221 222 Reader template = new InputStreamReader(getClass().getResourceAsStream("JSWGlue.stg")); 223 StringTemplateGroup group = new StringTemplateGroup(template, DefaultTemplateLexer.class); 224 StringTemplate glue = group.getInstanceOf("glue"); 225 glue.setAttribute("effectName", effectName); 226 glue.setAttribute("peerName", peerName); 227 glue.setAttribute("genericsDecl", genericsDecl.toString()); 228 glue.setAttribute("interfaceDecl", interfaceDecl.toString()); 229 glue.setAttribute("usercode", usercode.toString()); 230 glue.setAttribute("samplers", samplers.toString()); 231 glue.setAttribute("cleanup", cleanup.toString()); 232 glue.setAttribute("srcRects", srcRects.toString()); 233 glue.setAttribute("constants", constants.toString()); 234 glue.setAttribute("posDecls", posDecls.toString()); 235 glue.setAttribute("pixInitY", pixInitY.toString()); 236 glue.setAttribute("pixInitX", pixInitX.toString()); 237 glue.setAttribute("posIncrY", posIncrY.toString()); 238 glue.setAttribute("posInitY", posInitY.toString()); 239 glue.setAttribute("posIncrX", posIncrX.toString()); 240 glue.setAttribute("posInitX", posInitX.toString()); 241 glue.setAttribute("body", body); 242 return glue.toString(); 243 } 244 245 // TODO: need better mechanism for querying fields 246 private static char[] fields = {'x', 'y', 'z', 'w'}; 247 public static String getSuffix(int i) { |