Jason Sams | 7eecbf2 | 2010-02-23 15:27:51 -0800 | [diff] [blame] | 1 | #define NO_RS_FUNCS 1 |
| 2 | |
| 3 | #include "stdio.h" |
| 4 | #include "RenderScript.h" |
| 5 | #include <vector> |
| 6 | |
| 7 | struct Element; |
| 8 | |
| 9 | struct ElementField { |
| 10 | const char *name; |
| 11 | Element *e; |
| 12 | ElementField(const char *n, Element *_e) { |
| 13 | name = n; |
| 14 | e = _e; |
| 15 | } |
| 16 | ElementField() { |
| 17 | name = NULL; |
| 18 | e = NULL; |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | struct Element { |
| 23 | ElementField *fields; |
| 24 | size_t fieldCount; |
| 25 | const char *name; |
| 26 | bool generated; |
| 27 | |
| 28 | RsDataType compType; |
| 29 | uint32_t compVectorSize; |
| 30 | |
| 31 | Element() { |
| 32 | fields = NULL; |
| 33 | fieldCount = 0; |
| 34 | name = NULL; |
| 35 | generated = false; |
| 36 | compType = RS_TYPE_ELEMENT; |
| 37 | compVectorSize = 0; |
| 38 | } |
| 39 | |
| 40 | Element(uint32_t _fieldCount, const char *_name) { |
| 41 | fields = new ElementField[_fieldCount]; |
| 42 | fieldCount = _fieldCount; |
| 43 | name = _name; |
| 44 | generated = false; |
| 45 | compType = RS_TYPE_ELEMENT; |
| 46 | compVectorSize = 0; |
| 47 | } |
| 48 | |
| 49 | Element(RsDataType t, uint32_t s) { |
| 50 | fields = NULL; |
| 51 | fieldCount = 0; |
| 52 | name = NULL; |
| 53 | generated = false; |
| 54 | compType = t; |
| 55 | compVectorSize = s; |
| 56 | } |
| 57 | |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | static void genHeader(FILE *f, const char *packageName) |
| 62 | { |
| 63 | fprintf(f, "package %s;\n", packageName); |
| 64 | fprintf(f, "\n"); |
| 65 | fprintf(f, "import android.renderscript.*;\n"); |
| 66 | fprintf(f, "\n"); |
| 67 | fprintf(f, "\n"); |
| 68 | } |
| 69 | |
| 70 | static const char * RSTypeToJava(RsDataType dt) |
| 71 | { |
| 72 | switch(dt) { |
| 73 | //case RS_TYPE_FLOAT_16: return "float"; |
| 74 | case RS_TYPE_FLOAT_32: return "float"; |
| 75 | //case RS_TYPE_FLOAT_64: return "double"; |
| 76 | |
| 77 | case RS_TYPE_SIGNED_8: return "byte"; |
| 78 | case RS_TYPE_SIGNED_16: return "short"; |
| 79 | case RS_TYPE_SIGNED_32: return "int"; |
| 80 | //case RS_TYPE_SIGNED_64: return "long"; |
| 81 | |
| 82 | case RS_TYPE_UNSIGNED_8: return "short"; |
| 83 | case RS_TYPE_UNSIGNED_16: return "int"; |
| 84 | case RS_TYPE_UNSIGNED_32: return "long"; |
| 85 | //case RS_TYPE_UNSIGNED_64: return NULL; |
| 86 | |
| 87 | //case RS_TYPE_ELEMENT: return "android.renderscript.Element"; |
| 88 | //case RS_TYPE_TYPE: return "android.renderscript.Type"; |
| 89 | //case RS_TYPE_ALLOCATION: return "android.renderscript.Allocation"; |
| 90 | //case RS_TYPE_SAMPLER: return "android.renderscript.Sampler"; |
| 91 | //case RS_TYPE_SCRIPT: return "android.renderscript.Script"; |
| 92 | //case RS_TYPE_MESH: return "android.renderscript.Mesh"; |
| 93 | //case RS_TYPE_PROGRAM_FRAGMENT: return "android.renderscript.ProgramFragment"; |
| 94 | //case RS_TYPE_PROGRAM_VERTEX: return "android.renderscript.ProgramVertex"; |
| 95 | //case RS_TYPE_PROGRAM_RASTER: return "android.renderscript.ProgramRaster"; |
| 96 | //case RS_TYPE_PROGRAM_STORE: return "android.renderscript.ProgramStore"; |
| 97 | default: return NULL; |
| 98 | } |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | static const char * RSTypeToString(RsDataType dt) |
| 103 | { |
| 104 | switch(dt) { |
| 105 | case RS_TYPE_FLOAT_16: return "F16"; |
| 106 | case RS_TYPE_FLOAT_32: return "F32"; |
| 107 | case RS_TYPE_FLOAT_64: return "F64"; |
| 108 | |
| 109 | case RS_TYPE_SIGNED_8: return "I8"; |
| 110 | case RS_TYPE_SIGNED_16: return "I16"; |
| 111 | case RS_TYPE_SIGNED_32: return "I32"; |
| 112 | case RS_TYPE_SIGNED_64: return "I64"; |
| 113 | |
| 114 | case RS_TYPE_UNSIGNED_8: return "U8"; |
| 115 | case RS_TYPE_UNSIGNED_16: return "U16"; |
| 116 | case RS_TYPE_UNSIGNED_32: return "U32"; |
| 117 | case RS_TYPE_UNSIGNED_64: return "U64"; |
| 118 | |
| 119 | //case RS_TYPE_ELEMENT: return "android.renderscript.Element"; |
| 120 | //case RS_TYPE_TYPE: return "android.renderscript.Type"; |
| 121 | //case RS_TYPE_ALLOCATION: return "android.renderscript.Allocation"; |
| 122 | //case RS_TYPE_SAMPLER: return "android.renderscript.Sampler"; |
| 123 | //case RS_TYPE_SCRIPT: return "android.renderscript.Script"; |
| 124 | //case RS_TYPE_MESH: return "android.renderscript.Mesh"; |
| 125 | //case RS_TYPE_PROGRAM_FRAGMENT: return "android.renderscript.ProgramFragment"; |
| 126 | //case RS_TYPE_PROGRAM_VERTEX: return "android.renderscript.ProgramVertex"; |
| 127 | //case RS_TYPE_PROGRAM_RASTER: return "android.renderscript.ProgramRaster"; |
| 128 | //case RS_TYPE_PROGRAM_STORE: return "android.renderscript.ProgramStore"; |
| 129 | default: return NULL; |
| 130 | } |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | bool rsGenerateElementClass(const Element *e, const char *packageName, FILE *f) |
| 135 | { |
| 136 | genHeader(f, packageName); |
| 137 | |
| 138 | fprintf(f, "class Element_%s {\n", e->name); |
| 139 | |
| 140 | for (size_t ct=0; ct < e->fieldCount; ct++) { |
| 141 | const char *ts = RSTypeToJava(e->fields[ct].e->compType); |
| 142 | if (ts == NULL) { |
| 143 | return false; |
| 144 | } |
| 145 | fprintf(f, " public %s %s;\n", ts, e->fields[ct].name); |
| 146 | } |
| 147 | |
| 148 | fprintf(f, "\n"); |
| 149 | fprintf(f, " static Element getElement(RenderScript rs) {\n"); |
| 150 | fprintf(f, " Element.Builder eb = new Element.Builder(rs);\n"); |
| 151 | for (size_t ct=0; ct < e->fieldCount; ct++) { |
| 152 | const char *ts = RSTypeToString(e->fields[ct].e->compType); |
| 153 | fprintf(f, " eb.add(Element.USER_%s(rs), \"%s\");\n", ts, e->fields[ct].name); |
| 154 | } |
| 155 | fprintf(f, " return eb.create();\n"); |
| 156 | fprintf(f, " }\n"); |
| 157 | |
| 158 | fprintf(f, " static Allocation createAllocation(RenderScript rs) {\n"); |
| 159 | fprintf(f, " Element e = getElement(rs);\n"); |
| 160 | fprintf(f, " Allocation a = Allocation.createSized(rs, e, 1);\n"); |
| 161 | fprintf(f, " return a;\n"); |
| 162 | fprintf(f, " }\n"); |
| 163 | |
| 164 | |
| 165 | fprintf(f, " void copyToAllocation(Allocation a) {\n"); |
| 166 | fprintf(f, " mIOBuffer.reset();\n"); |
| 167 | for (size_t ct=0; ct < e->fieldCount; ct++) { |
| 168 | const char *ts = RSTypeToString(e->fields[ct].e->compType); |
| 169 | fprintf(f, " mIOBuffer.add%s(%s);\n", ts, e->fields[ct].name); |
| 170 | } |
| 171 | fprintf(f, " a.data(mIOBuffer.getData());\n"); |
| 172 | fprintf(f, " }\n"); |
| 173 | |
| 174 | |
| 175 | |
| 176 | fprintf(f, " private FieldPacker mIOBuffer[];\n"); |
| 177 | fprintf(f, " public Element_%s() {\n", e->name); |
| 178 | fprintf(f, " mIOBuffer = new FieldPacker(%i);\n", 100/*element->getSizeBytes()*/); |
| 179 | fprintf(f, " }\n"); |
| 180 | |
| 181 | |
| 182 | fprintf(f, "}\n"); |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | bool rsGenerateElementClassFile(Element *e, const char *packageName) |
| 188 | { |
| 189 | char buf[1024]; |
| 190 | sprintf(buf, "Element_%s.java", e->name); |
| 191 | printf("Creating file %s \n", buf); |
| 192 | FILE *f = fopen(buf, "w"); |
| 193 | bool ret = rsGenerateElementClass(e, packageName, f); |
| 194 | fclose(f); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | /* |
| 202 | bool rsGenerateScriptClass(const ScriptC *script, const char *packageName, FILE *f) |
| 203 | { |
| 204 | genHeader(f, packageName); |
| 205 | |
| 206 | fprintf(f, "class ScriptC_%s {\n", script->getName()); |
| 207 | |
| 208 | |
| 209 | |
| 210 | ObjectBaseRef<const Type> mTypes[MAX_SCRIPT_BANKS]; |
| 211 | String8 mSlotNames[MAX_SCRIPT_BANKS]; |
| 212 | bool mSlotWritable[MAX_SCRIPT_BANKS]; |
| 213 | |
| 214 | |
| 215 | } |
| 216 | */ |
| 217 | |
| 218 | |
| 219 | |
| 220 | int main(int argc, const char *argv) |
| 221 | { |
| 222 | Element *u8 = new Element(RS_TYPE_UNSIGNED_8, 1); |
| 223 | Element *i32 = new Element(RS_TYPE_SIGNED_32, 1); |
| 224 | Element *f32 = new Element(RS_TYPE_FLOAT_32, 1); |
| 225 | |
| 226 | Element *e_Pixel = new Element(4, "Pixel"); |
| 227 | e_Pixel->fields[0].e = u8; |
| 228 | e_Pixel->fields[0].name = "a"; |
| 229 | e_Pixel->fields[1].e = u8; |
| 230 | e_Pixel->fields[1].name = "b"; |
| 231 | e_Pixel->fields[2].e = u8; |
| 232 | e_Pixel->fields[2].name = "g"; |
| 233 | e_Pixel->fields[3].e = u8; |
| 234 | e_Pixel->fields[3].name = "r"; |
| 235 | |
| 236 | Element *e_Params = new Element(5, "Params"); |
| 237 | e_Params->fields[0].e = i32; |
| 238 | e_Params->fields[0].name = "inHeight"; |
| 239 | e_Params->fields[1].e = i32; |
| 240 | e_Params->fields[1].name = "inWidth"; |
| 241 | e_Params->fields[2].e = i32; |
| 242 | e_Params->fields[2].name = "outHeight"; |
| 243 | e_Params->fields[3].e = i32; |
| 244 | e_Params->fields[3].name = "outWidth"; |
| 245 | e_Params->fields[4].e = f32; |
| 246 | e_Params->fields[4].name = "threshold"; |
| 247 | |
| 248 | |
| 249 | printf("1\n"); |
| 250 | rsGenerateElementClassFile(e_Pixel, "android"); |
| 251 | rsGenerateElementClassFile(e_Params, "android"); |
| 252 | |
| 253 | } |
| 254 | |