static void AddParameter(JSON &json, CGparameter param) { const char * const parameterName = cgGetParameterName(param); if (sVerbose) { puts(parameterName); } json.AddObject(parameterName); const CGtype type = cgGetParameterType(param); const CGtype baseType = cgGetParameterBaseType(param); int numRows = cgGetParameterRows(param); const int numColumns = cgGetParameterColumns(param); if (CG_ARRAY == type) { const int totalArraySize = cgGetArrayTotalSize(param); numRows *= totalArraySize; } json.AddString("type", cgGetTypeString(baseType)); if (1 < numRows) { json.AddValue("rows", numRows); } if (1 < numColumns) { json.AddValue("columns", numColumns); } const int maxNumElements = (numColumns * numRows); int n; if (CG_FLOAT == baseType) { float * const values = (float *)malloc(maxNumElements * sizeof(float)); const int numValues = cgGetParameterValuefr(param, maxNumElements, values); if (numValues) { for (n = 0; n < numValues; n++) { if (values[n] != 0.0f) { break; } } if (n < numValues) { json.AddArray("values", true); json.BeginData(true); for (n = 0; n < numValues; n++) { json.AddData(values[n]); } json.EndData(); json.CloseArray(true); } } free(values); } else if (CG_INT == baseType) { int * const values = (int *)malloc(maxNumElements * sizeof(int)); const int numValues = cgGetParameterValueir(param, maxNumElements, values); if (numValues) { for (n = 0; n < numValues; n++) { if (values[n]) { break; } } if (n < numValues) { json.AddArray("values", true); json.BeginData(true); for (n = 0; n < numValues; n++) { json.AddData(values[n]); } json.EndData(); json.CloseArray(true); } } free(values); } else if (CG_BOOL == baseType) { int * const values = (int *)malloc(maxNumElements * sizeof(int)); const int numValues = cgGetParameterValueir(param, maxNumElements, values); if (numValues) { for (n = 0; n < numValues; n++) { if (values[n]) { break; } } if (n < numValues) { json.AddArray("values", true); json.BeginData(true); for (n = 0; n < numValues; n++) { json.AddData(values[n]); } json.EndData(); json.CloseArray(true); } } free(values); } json.CloseObject(); // parameter }
static bool AddPass(CGtechnique technique, JSON &json, CGpass pass, UniformsMap &uniformRemapping) { bool success = true; json.AddObject(NULL); const char * const passName = cgGetPassName(pass); if (NULL != passName) { json.AddString("name", passName); } bool firstParameter = true; #if CG_VERSION_NUM >= 3000 const int CG_NUMBER_OF_DOMAINS = (CG_TESSELLATION_EVALUATION_DOMAIN + 1); #endif for (int domain = CG_FIRST_DOMAIN; domain < CG_NUMBER_OF_DOMAINS; domain++) { const CGprogram program = cgGetPassProgram(pass, (CGdomain)domain); if (NULL != program) { const char * const programString = cgGetProgramString(program, CG_COMPILED_PROGRAM); CGparameter param = cgGetFirstParameter(program, CG_GLOBAL); while (NULL != param) { if (cgIsParameterUsed(param, program) && CG_UNIFORM == cgGetParameterVariability(param)) { if (firstParameter) { firstParameter = false; json.AddArray("parameters", true); json.BeginData(true); } const char * const paramName = cgGetParameterName(param); AddMappedParameter(json, paramName, programString, uniformRemapping); } param = cgGetNextParameter(param); } param = cgGetFirstParameter(program, CG_PROGRAM); while (NULL != param) { if (cgIsParameterUsed(param, program) && CG_UNIFORM == cgGetParameterVariability(param)) { if (firstParameter) { firstParameter = false; json.AddArray("parameters", true); json.BeginData(true); } const char * const paramName = cgGetParameterName(param); AddMappedParameter(json, paramName, programString, uniformRemapping); } param = cgGetNextParameter(param); } } } if (!firstParameter) { json.EndData(); json.CloseArray(true); // parameters } json.AddArray("semantics", true); json.BeginData(true); CGprogram vertexProgram = cgGetPassProgram(pass, CG_VERTEX_DOMAIN); CGparameter vertexInputParameter = cgGetFirstLeafParameter(vertexProgram, CG_PROGRAM); while (NULL != vertexInputParameter) { const CGenum variability = cgGetParameterVariability(vertexInputParameter); if (CG_VARYING == variability) { const CGenum direction = cgGetParameterDirection(vertexInputParameter); if (CG_IN == direction || CG_INOUT == direction) { const char * const semantic = cgGetParameterSemantic(vertexInputParameter); json.AddData(semantic, strlen(semantic)); } } vertexInputParameter = cgGetNextLeafParameter(vertexInputParameter); } json.EndData(); json.CloseArray(true); // semantics json.AddObject("states"); CGstateassignment state = cgGetFirstStateAssignment(pass); if (NULL != state) { do { success &= AddState(json, state); state = cgGetNextStateAssignment(state); } while (NULL != state); } json.CloseObject(); // states json.AddArray("programs", true); json.BeginData(true); for (int domain = CG_FIRST_DOMAIN; domain < CG_NUMBER_OF_DOMAINS; domain++) { const CGprogram program = cgGetPassProgram(pass, (CGdomain)domain); if (NULL != program) { const char * const entryPoint = cgGetProgramString(program, CG_PROGRAM_ENTRY); json.AddData(entryPoint, strlen(entryPoint)); } else if (domain == CG_VERTEX_DOMAIN) { ErrorMessage("%s : No vertex program.", cgGetTechniqueName(technique)); success = false; } else if(domain == CG_FRAGMENT_DOMAIN) { ErrorMessage("%s : No fragment program.", cgGetTechniqueName(technique)); success = false; } } json.EndData(); json.CloseArray(true); // programs json.CloseObject(); // pass return success; }