/* * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.scenario.effect.compiler.model; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import static com.sun.scenario.effect.compiler.model.Precision.*; import static com.sun.scenario.effect.compiler.model.Type.*; /** * Maintains the sets of core (built-in) functions and variables. */ public class CoreSymbols { private static Set vars = new HashSet(); private static Set funcs = new HashSet(); static Set getAllVariables() { return vars; } static Set getAllFunctions() { return funcs; } public static Function getFunction(String name, List ptypes) { return SymbolTable.getFunctionForSignature(funcs, name, ptypes); } static { // pos0/1, pixcoord, and jsl_vertexColor are declared "const" // (read-only) to prevent accidental assignment // TODO: should probably add "jsl_" prefix to all of these to make // it clear that they are special variables... declareVariable("pos0", FLOAT2, null, true); declareVariable("pos1", FLOAT2, null, true); declareVariable("pixcoord", FLOAT2, null, true); declareVariable("jsl_vertexColor", FLOAT4, LOWP, true); declareVariable("color", FLOAT4, LOWP, false); // float4 sample(sampler s, float2 loc) declareFunction(FLOAT4, "sample", SAMPLER, "s", FLOAT2, "loc"); // float4 sample(lsampler s, float2 loc) declareFunction(FLOAT4, "sample", LSAMPLER, "s", FLOAT2, "loc"); // float4 sample(fsampler s, float2 loc) declareFunction(FLOAT4, "sample", FSAMPLER, "s", FLOAT2, "loc"); // int intcast(float x) declareFunction(INT, "intcast", FLOAT, "x"); // bool any( x) // GLSL only supports: bool any( x); where N := 2, 3, 4 // HLSL supports: bool any( x) declareOverloadsBool("any"); // min( x, y) // min( x, float y) declareOverloadsMinMax("min"); // max( x, y) // max( x, float y) declareOverloadsMinMax("max"); // clamp( val, min, max) // clamp( val, float min, float max) declareOverloadsClamp(); // smoothstep( min, max, val) // smoothstep(float min, float max, val) declareOverloadsSmoothstep(); // abs( x) declareOverloadsSimple("abs"); // floor( x) declareOverloadsSimple("floor"); // ceil( x) declareOverloadsSimple("ceil"); // fract( x) declareOverloadsSimple("fract"); // sign( x) declareOverloadsSimple("sign"); // sqrt( x) declareOverloadsSimple("sqrt"); // sin( x) declareOverloadsSimple("sin"); // cos( x) declareOverloadsSimple("cos"); // tan( x) declareOverloadsSimple("tan"); // pow( x, y) declareOverloadsSimple2("pow"); // mod( x, y) // mod( x, float y) declareOverloadsMinMax("mod"); // float dot( x, y) declareOverloadsFloat2("dot"); // float distance( x, y) declareOverloadsFloat2("distance"); // float length( x) declareOverloadsFloat("length"); // mix( x, y, a) // mix( x, y, float a) declareOverloadsMix(); // normalize( x) declareOverloadsSimple("normalize"); // ddx( p) declareOverloadsSimple("ddx"); // ddy( p) declareOverloadsSimple("ddy"); } private static void declareVariable(String name, Type type, Precision precision, boolean readonly) { Qualifier qual = readonly ? Qualifier.CONST : null; vars.add(new Variable(name, type, qual, precision, -1, -1, null, false)); } private static void declareFunction(Type returnType, String name, Object... params) { List paramList = new ArrayList(); if (params.length % 2 != 0) { throw new InternalError("Params array length must be even"); } for (int i = 0; i < params.length; i+=2) { if (!(params[i+0] instanceof Type) || !(params[i+1] instanceof String)) { throw new InternalError("Params must be specified as (Type,String) pairs"); } paramList.add(new Param((String)params[i+1], (Type)params[i])); } funcs.add(new Function(name, returnType, paramList)); } private static void declareOverloadsSimple(String name) { declareFunction(FLOAT, name, FLOAT, "x"); declareFunction(FLOAT2, name, FLOAT2, "x"); declareFunction(FLOAT3, name, FLOAT3, "x"); declareFunction(FLOAT4, name, FLOAT4, "x"); } private static void declareOverloadsSimple2(String name) { declareFunction(FLOAT, name, FLOAT, "x", FLOAT, "y"); declareFunction(FLOAT2, name, FLOAT2, "x", FLOAT2, "y"); declareFunction(FLOAT3, name, FLOAT3, "x", FLOAT3, "y"); declareFunction(FLOAT4, name, FLOAT4, "x", FLOAT4, "y"); } private static void declareOverloadsMinMax(String name) { declareFunction(FLOAT, name, FLOAT, "x", FLOAT, "y"); declareFunction(FLOAT2, name, FLOAT2, "x", FLOAT2, "y"); declareFunction(FLOAT3, name, FLOAT3, "x", FLOAT3, "y"); declareFunction(FLOAT4, name, FLOAT4, "x", FLOAT4, "y"); declareFunction(FLOAT2, name, FLOAT2, "x", FLOAT, "y"); declareFunction(FLOAT3, name, FLOAT3, "x", FLOAT, "y"); declareFunction(FLOAT4, name, FLOAT4, "x", FLOAT, "y"); } private static void declareOverloadsClamp() { final String name = "clamp"; declareFunction(FLOAT, name, FLOAT, "val", FLOAT, "min", FLOAT, "max"); declareFunction(FLOAT2, name, FLOAT2, "val", FLOAT2, "min", FLOAT2, "max"); declareFunction(FLOAT3, name, FLOAT3, "val", FLOAT3, "min", FLOAT3, "max"); declareFunction(FLOAT4, name, FLOAT4, "val", FLOAT4, "min", FLOAT4, "max"); declareFunction(FLOAT2, name, FLOAT2, "val", FLOAT, "min", FLOAT, "max"); declareFunction(FLOAT3, name, FLOAT3, "val", FLOAT, "min", FLOAT, "max"); declareFunction(FLOAT4, name, FLOAT4, "val", FLOAT, "min", FLOAT, "max"); } private static void declareOverloadsSmoothstep() { final String name = "smoothstep"; declareFunction(FLOAT, name, FLOAT, "min", FLOAT, "max", FLOAT, "val"); declareFunction(FLOAT2, name, FLOAT2, "min", FLOAT2, "max", FLOAT2, "val"); declareFunction(FLOAT3, name, FLOAT3, "min", FLOAT3, "max", FLOAT3, "val"); declareFunction(FLOAT4, name, FLOAT4, "min", FLOAT4, "max", FLOAT4, "val"); declareFunction(FLOAT2, name, FLOAT, "min", FLOAT, "max", FLOAT2, "val"); declareFunction(FLOAT3, name, FLOAT, "min", FLOAT, "max", FLOAT3, "val"); declareFunction(FLOAT4, name, FLOAT, "min", FLOAT, "max", FLOAT4, "val"); } private static void declareOverloadsMix() { final String name = "mix"; declareFunction(FLOAT, name, FLOAT, "x", FLOAT, "y", FLOAT, "a"); declareFunction(FLOAT2, name, FLOAT2, "x", FLOAT2, "y", FLOAT2, "a"); declareFunction(FLOAT3, name, FLOAT3, "x", FLOAT3, "y", FLOAT3, "a"); declareFunction(FLOAT4, name, FLOAT4, "x", FLOAT4, "y", FLOAT4, "a"); declareFunction(FLOAT2, name, FLOAT2, "x", FLOAT2, "y", FLOAT, "a"); declareFunction(FLOAT3, name, FLOAT3, "x", FLOAT3, "y", FLOAT, "a"); declareFunction(FLOAT4, name, FLOAT4, "x", FLOAT4, "y", FLOAT, "a"); } private static void declareOverloadsBool(String name) { declareFunction(BOOL, name, BOOL2, "x"); declareFunction(BOOL, name, BOOL3, "x"); declareFunction(BOOL, name, BOOL4, "x"); } private static void declareOverloadsFloat(String name) { declareFunction(FLOAT, name, FLOAT, "x"); declareFunction(FLOAT, name, FLOAT2, "x"); declareFunction(FLOAT, name, FLOAT3, "x"); declareFunction(FLOAT, name, FLOAT4, "x"); } private static void declareOverloadsFloat2(String name) { declareFunction(FLOAT, name, FLOAT, "x", FLOAT, "y"); declareFunction(FLOAT, name, FLOAT2, "x", FLOAT2, "y"); declareFunction(FLOAT, name, FLOAT3, "x", FLOAT3, "y"); declareFunction(FLOAT, name, FLOAT4, "x", FLOAT4, "y"); } }