Example #1
0
cgfxPass::cgfxPass(
    CGpass                  pass,
    const cgfxProfile*      profile
)
 :	fPass( pass),
	fProgram( NULL),
	fParameters( NULL),
    fDefaultProfile("default", pass),
	fNext( NULL)
{
	if( pass)
	{
		fName = cgGetPassName( pass);
		CGstateassignment stateAssignment = cgGetFirstStateAssignment( pass);
		cgfxVaryingParameter** nextParameter = &fParameters;
		while( stateAssignment )
		{
			CGstate state = cgGetStateAssignmentState( stateAssignment);
			if( cgGetStateType( state) == CG_PROGRAM_TYPE && 
					( stricmp( cgGetStateName( state), "vertexProgram") == 0 ||
					  stricmp( cgGetStateName( state), "vertexShader") == 0))
			{
				fProgram = cgGetProgramStateAssignmentValue( stateAssignment);
				if( fProgram)
				{
					CGparameter parameter = cgGetFirstParameter( fProgram, CG_PROGRAM);
					while( parameter)
					{
                        cgfxVaryingParameter::addRecursive( parameter, nextParameter);
						parameter = cgGetNextParameter( parameter);
					}
				}
			}
            setProfile(profile);
			stateAssignment = cgGetNextStateAssignment( stateAssignment);
		}
	}
}
Example #2
0
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;
}
Example #3
0
//
// Scan the technique for passes which use blending
//
bool cgfxTechnique::hasBlending(CGtechnique technique)
{
	// Assume not blending
	bool hasBlending = false;

	// Check for : BlendEnable=true, BlendFunc=something valid on the first pass only.
	//
	// We ignore any depth enable and functions for now...
	//
	CGpass cgPass = cgGetFirstPass(technique);
	bool foundBlendEnabled = false;
	bool foundBlendFunc = false;
	if (cgPass)
	{
		CGstateassignment stateAssignment = cgGetFirstStateAssignment(cgPass);
		while ( stateAssignment )
		{
			CGstate state = cgGetStateAssignmentState( stateAssignment);
			const char *stateName = cgGetStateName(state);

			// Check for blend enabled.
			if (!foundBlendEnabled && stricmp( stateName, "BlendEnable") == 0)
			{
				int numValues = 0;
				const CGbool *values = cgGetBoolStateAssignmentValues(stateAssignment, &numValues);
				if (values && numValues)
				{
					if (values[0])
					{
						foundBlendEnabled = true;
					}
				}
			}

			// Check for valid blend function
			else if (!foundBlendFunc && ( stricmp( stateName, "BlendFunc") == 0 ||
										  stricmp( stateName, "BlendFuncSeparate") == 0 ))
			{
				int numValues = 0;
				const int * values = cgGetIntStateAssignmentValues(stateAssignment, &numValues);
				if (values)
				{
#if defined(CGFX_DEBUG_BLEND_FUNCTIONS)
					/*
					#define GL_SRC_COLOR                      0x0300 = 768
					#define GL_ONE_MINUS_SRC_COLOR            0x0301 = 769
					#define GL_SRC_ALPHA                      0x0302 = 770
					#define GL_ONE_MINUS_SRC_ALPHA            0x0303 = 771
					#define GL_DST_ALPHA                      0x0304 = 772
					#define GL_ONE_MINUS_DST_ALPHA            0x0305 = 773
					*/
					MString blendStringTable[6] =
					{
						"GL_SRC_COLOR", // SrcColor
						"GL_ONE_MINUS_SRC_COLOR", // OneMinusSrcColor
						"GL_SRC_ALPHA", // SrcAlpha
						"GL_ONE_MINUS_SRC_ALPHA", // OneMinusSrcAlpha
						"GL_DST_ALPHA", // DstAlpha
						"GL_ONE_MINUS_DST_ALPHA" // OneMinusDstAlpha
					};
#endif
					for (int i=0; i<numValues; i++)
					{
						if ((values[i] >= GL_SRC_COLOR) && (values[i] <= GL_ONE_MINUS_DST_ALPHA))
						{
#if defined(CGFX_DEBUG_BLEND_FUNCTIONS)
							printf("Found blend function = %s, %s\n",
							blendStringTable[ values[0]-GL_SRC_COLOR].asChar(),
							blendStringTable[ values[1]-GL_SRC_COLOR].asChar());
#endif
							foundBlendFunc = true;
							break;
						}
					}
				}
			}
			hasBlending = foundBlendEnabled && foundBlendFunc;
			if (hasBlending)
				break;
			stateAssignment = cgGetNextStateAssignment( stateAssignment);
		}
	}

    return hasBlending;
}