Exemplo n.º 1
0
void Cg::Init(void)
{

  context = NULL;
	vertexProgram = NULL;
	vertexProfile = CG_PROFILE_ARBVP1;
	fragmentProgram = NULL;
	fragmentProfile = CG_PROFILE_ARBFP1;



	//vertex用
	BNUMParam = NULL;
	BRParam = NULL;
	INIBNParam = NULL;



	//fragment用
	vdecalParam   = NULL;
	fdecalParam   = NULL;
	globalAmbientParam = NULL;
	lightColorParam = NULL;
	lightPositionParam = NULL;
	eyePositionParam = NULL;
	KeParam = NULL;
	KaParam = NULL;
	KdParam = NULL;
	KsParam = NULL;
	shininessParam = NULL;
	matModelViewNormalParam = NULL;
	cameraParam = NULL;
	stepsizeParam = NULL;
	volExtentMinParam = NULL;
	volExtentMaxParam = NULL;
	rsclParam = NULL;
	resoColorParam = NULL;
	renderAlphaParam = NULL;
	renderBetaParam  = NULL;
	farSliceParam = NULL;
	rayLoopParam = NULL;
	srcDivParam  = NULL;
	BLParam      = NULL;
	transfer_functionParam = NULL;

	//スライス用
	vertex_sliceProgram = NULL;

	BR2Param = NULL;
	BNUM2Param = NULL;
	INIBN2Param = NULL;
	dPlaneStartParam  = NULL;
	frontIdxParam     = NULL;
	vecViewParam       = NULL;
	nSequenceTemp    = NULL;
	vecVerticesTemp = NULL;
	v1Temp = NULL;
	v2Temp = NULL;

	//occlusion fragment用
	fragment_occlusionProgram = NULL;
	fdecal2Param = NULL;


	vertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
	fprintf(stderr, "Video card supports : %d.\n", vertexProfile);
	CheckCgError();

	fragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
	fprintf(stderr, "Video card supports : %d.\n", fragmentProfile);
	CheckCgError();


	context = cgCreateContext();
	CheckCgError();


	vertexProgram= cgCreateProgramFromFile(context, CG_SOURCE, "shader.cg",vertexProfile, "vertex", NULL);//TODO:

	CheckCgError();

	if(vertexProgram != NULL) {
		cgGLLoadProgram(vertexProgram);CheckCgError();
		BNUMParam = cgGetNamedParameter(vertexProgram, "BNUM");CheckCgError();
		BRParam = cgGetNamedParameter(vertexProgram, "BR");CheckCgError();
		INIBNParam = cgGetNamedParameter(vertexProgram, "INIBN");CheckCgError();
	}

	fragmentProgram= cgCreateProgramFromFile(context, CG_SOURCE, "shader.cg",fragmentProfile, "fragment", NULL);
	CheckCgError();

	if(fragmentProgram != NULL)
	{
		cgGLLoadProgram(fragmentProgram);
		CheckCgError();


		vdecalParam = cgGetNamedParameter(fragmentProgram,"vdecal");CheckCgError();	  
		fdecalParam = cgGetNamedParameter(fragmentProgram,"fdecal");CheckCgError();	  

		globalAmbientParam = cgGetNamedParameter(fragmentProgram, "globalAmbient");CheckCgError();
		lightColorParam = cgGetNamedParameter(fragmentProgram, "lightColor");CheckCgError();
		lightPositionParam = cgGetNamedParameter(fragmentProgram, "lightPosition");CheckCgError();
		eyePositionParam = cgGetNamedParameter(fragmentProgram, "eyePosition");CheckCgError();
		KeParam = cgGetNamedParameter(fragmentProgram, "Ke");CheckCgError();
		KaParam = cgGetNamedParameter(fragmentProgram, "Ka");CheckCgError();
		KdParam = cgGetNamedParameter(fragmentProgram, "Kd");CheckCgError();
		KsParam = cgGetNamedParameter(fragmentProgram, "Ks");CheckCgError();
		shininessParam = cgGetNamedParameter(fragmentProgram, "shininess");CheckCgError();
		matModelViewNormalParam = cgGetNamedParameter(fragmentProgram, "matModelViewNormal");CheckCgError();

		cameraParam = cgGetNamedParameter(fragmentProgram, "camera");CheckCgError();
		stepsizeParam = cgGetNamedParameter(fragmentProgram, "stepsize");CheckCgError();
		volExtentMinParam = cgGetNamedParameter(fragmentProgram, "volExtentMin");CheckCgError();
		volExtentMaxParam = cgGetNamedParameter(fragmentProgram, "volExtentMax");CheckCgError();

		rsclParam = cgGetNamedParameter(fragmentProgram, "rscl");CheckCgError();
		resoColorParam = cgGetNamedParameter(fragmentProgram, "resoColor");CheckCgError();

		renderAlphaParam = cgGetNamedParameter(fragmentProgram, "renderAlpha");CheckCgError();
		renderBetaParam = cgGetNamedParameter(fragmentProgram, "renderBeta");CheckCgError();

		farSliceParam = cgGetNamedParameter(fragmentProgram, "farSlice");CheckCgError();

		rayLoopParam = cgGetNamedParameter(fragmentProgram, "rayLoop");CheckCgError();

		srcDivParam = cgGetNamedParameter(fragmentProgram, "srcDiv");CheckCgError();

		BLParam = cgGetNamedParameter(fragmentProgram, "BL");CheckCgError();
		transfer_functionParam = cgGetNamedParameter(fragmentProgram,"transfer_function");CheckCgError();
		//cgGLSetTextureParameter(transfer_functionParam,preintName);CheckCgError();
	}

	vertex_sliceProgram= cgCreateProgramFromFile(context, CG_SOURCE, "shader.cg",vertexProfile, "vertex_slice", NULL);CheckCgError();


	CheckCgError();
	if(vertex_sliceProgram != NULL) {
		cgGLLoadProgram(vertex_sliceProgram);
		CheckCgError();


		frontIdxParam = cgGetNamedParameter(vertex_sliceProgram, "frontIdx");
		CheckCgError();
		dPlaneStartParam = cgGetNamedParameter(vertex_sliceProgram, "dPlaneStart");
		CheckCgError();
		vecViewParam = cgGetNamedParameter(vertex_sliceProgram, "vecView");
		CheckCgError();


		nSequenceTemp = cgGetNamedParameter(vertex_sliceProgram, "nSequence");
		CheckCgError();
		for(int i=0;i< 64;i++)
			nSequenceParam[i] = cgGetArrayParameter(nSequenceTemp,i);
		CheckCgError();
		vecVerticesTemp = cgGetNamedParameter(vertex_sliceProgram, "vecVertices");
		CheckCgError();
		for(int i=0 ;i< 8;i++)
			vecVerticesParam[i] = cgGetArrayParameter(vecVerticesTemp,i);
		CheckCgError();
		v1Temp = cgGetNamedParameter(vertex_sliceProgram, "v1");
		CheckCgError();
		for(int i= 0;i<24;i++)
			v1Param[i] = cgGetArrayParameter(v1Temp,i);
		CheckCgError();
		v2Temp = cgGetNamedParameter(vertex_sliceProgram, "v2");
		CheckCgError();
		for(int i= 0;i<24;i++)
			v2Param[i] = cgGetArrayParameter(v2Temp,i);
		CheckCgError();

		BNUM2Param = cgGetNamedParameter(vertex_sliceProgram, "BNUM");
		CheckCgError();

		BR2Param = cgGetNamedParameter(vertex_sliceProgram, "BR");
		CheckCgError();



		INIBN2Param = cgGetNamedParameter(vertex_sliceProgram, "INIBN");
		CheckCgError();
	}



	//オクルージョンfragment用
	fragment_occlusionProgram= cgCreateProgramFromFile(context, CG_SOURCE, "shader.cg",fragmentProfile, "fragment_occlusion", NULL);CheckCgError();

	if(fragment_occlusionProgram != NULL)
	{
		cgGLLoadProgram(fragment_occlusionProgram);
		CheckCgError();


		fdecal2Param = cgGetNamedParameter(fragmentProgram,"fdecal");
		CheckCgError();	  

	}
	

	this->SetParameter(this->srcDivParam,SRCDIV);


	/*
	MAXBL計算
	*/
	int MAXBL;
	MAXBL = max(BLX,BLY);
	MAXBL = max(MAXBL,BLZ);
	int MAXINI;
	MAXINI = max(INIBLX,INIBLY);
	MAXINI = max(MAXINI,INIBLZ);

	Block::brX = (float)BLX/(float)MAXBL;
	Block::brY = (float)BLY/(float)MAXBL;
	Block::brZ = (float)BLZ/(float)MAXBL;

	
	Block::iniX = (float)INIBLX/(float)MAXINI;
	Block::iniY = (float)INIBLY/(float)MAXINI;
	Block::iniZ = (float)INIBLZ/(float)MAXINI;

	float bl[3] = {BLX,BLY,BLZ};//bl=(64,64,64) 1ブロックあたりのボクセルの数を示すと思われる。

	this->SetParameter(BLParam,bl);

	float br[3] = {Block::brX,Block::brY,Block::brZ};//br=(1,1,1)
	printf("br(%.lf,%.lf,%.lf)",br[0],br[1],br[2]);
	this->SetParameter(BRParam,br);
	this->SetParameter(BR2Param,br);

	float ini[3] = {Block::iniX,Block::iniY,Block::iniZ};//ini=(1,1,1)
	//printf("ini(%.lf,%.lf,%.lf)\n",ini[0],ini[1],ini[2]);
	this->SetParameter(INIBNParam,ini);
	this->SetParameter(INIBN2Param,ini);

	float volExtentMin[3] = {0.0f*Block::brX,0.0f*Block::brY,0.0f*Block::brZ};
	//printf("br(%.lf,%.lf,%.lf)\n",Block::brX,Block::brY,Block::brZ);
	this->SetParameter(volExtentMinParam , volExtentMin);


	float volExtentMax[3] = {1.0f*Block::brX,1.0f*Block::brY,1.0f*Block::brZ};
	this->SetParameter(volExtentMaxParam , volExtentMax);


	for(int i = 0;i<24;i++)
	{
		this->SetParameter(v1Param[i],(float)v1[i]);
		this->SetParameter(v2Param[i],(float)v2[i]);

	}
	for(int i = 0;i<64;i++)
		this->SetParameter(nSequenceParam[i],(float)nSequence[i]);
	for(int i= 0;i<8;i++)
		this->SetParameter(vecVerticesParam[i],vecVertices[i]);
}
bool CgShaderProgramGL::setConstant(const io::stringc &Name, const f32* Buffer, s32 Count)
{
    if (!Buffer)
        return false;

    /* Get top-level parameter */
    CGparameter Param = cgGetNamedParameter(cgProgram_, Name.c_str());

    if (!Param)
        return false;

    /* Get array parameter */
    if (cgGetParameterType(Param) != CG_ARRAY)
        return false;

    s32 ArraySize = cgGetArraySize(Param, 0);

    for (s32 i = 0; i < ArraySize; ++i)
    {
        /* Get array element parameter */
        CGparameter ElementParam = cgGetArrayParameter(Param, i);

        switch (cgGetParameterType(ElementParam))
        {
        case CG_FLOAT:
            cgGLSetParameterArray1f(Param, 0, Count, Buffer);
            return true;
        case CG_FLOAT2:
            cgGLSetParameterArray2f(Param, 0, Count/2, Buffer);
            return true;
        case CG_FLOAT3:
            cgGLSetParameterArray3f(Param, 0, Count/3, Buffer);
            return true;
        case CG_FLOAT4:
            cgGLSetParameterArray4f(Param, 0, Count/4, Buffer);
            return true;
        case CG_FLOAT4x4:
            cgGLSetMatrixParameterArrayfc(Param, 0, Count/16, Buffer);
            return true;

        case CG_STRUCT:
        {
            /* Get structure field parameter */
            CGparameter FieldParam = cgGetFirstStructParameter(ElementParam);

            while (FieldParam)
            {
                switch (cgGetParameterType(FieldParam))
                {
                case CG_FLOAT:
                    cgGLSetParameter1f(FieldParam, *Buffer);
                    Buffer += 1;
                    break;
                case CG_FLOAT2:
                    cgGLSetParameter2fv(FieldParam, Buffer);
                    Buffer += 2;
                    break;
                case CG_FLOAT3:
                    cgGLSetParameter3fv(FieldParam, Buffer);
                    Buffer += 3;
                    break;
                case CG_FLOAT4:
                    cgGLSetParameter4fv(FieldParam, Buffer);
                    Buffer += 4;
                    break;
                case CG_FLOAT4x4:
                    cgGLSetMatrixParameterfc(FieldParam, Buffer);
                    Buffer += 16;
                    break;
                case CG_INT:
                    cgSetParameter1i(FieldParam, *((s32*)Buffer));
                    Buffer += 1;
                    break;
                default:
                    break;
                }

                FieldParam = cgGetNextParameter(FieldParam);
            }
        }
        break;

        default:
            break;
        }
    }

    return true;
}
Exemplo n.º 3
0
	//---------------------------------------------------------------------
	void CgProgram::recurseParams(CGparameter parameter, size_t contextArraySize)
	{
		while (parameter != 0)
		{
			// Look for uniform parameters only
			// Don't bother enumerating unused parameters, especially since they will
			// be optimised out and therefore not in the indexed versions
			CGtype paramType = cgGetParameterType(parameter);

			if (cgGetParameterVariability(parameter) == CG_UNIFORM &&
				paramType != CG_SAMPLER1D &&
				paramType != CG_SAMPLER2D &&
				paramType != CG_SAMPLER3D &&
				paramType != CG_SAMPLERCUBE &&
				paramType != CG_SAMPLERRECT &&
				cgGetParameterDirection(parameter) != CG_OUT && 
				cgIsParameterReferenced(parameter))
			{
				int arraySize;

				switch(paramType)
				{
				case CG_STRUCT:
					recurseParams(cgGetFirstStructParameter(parameter));
					break;
				case CG_ARRAY:
					// Support only 1-dimensional arrays
					arraySize = cgGetArraySize(parameter, 0);
					recurseParams(cgGetArrayParameter(parameter, 0), (size_t)arraySize);
					break;
				default:
					// Normal path (leaf)
					String paramName = cgGetParameterName(parameter);
					size_t logicalIndex = cgGetParameterResourceIndex(parameter);

					// Get the parameter resource, to calculate the physical index
					CGresource res = cgGetParameterResource(parameter);
					bool isRegisterCombiner = false;
					size_t regCombinerPhysicalIndex = 0;
					switch (res)
					{
					case CG_COMBINER_STAGE_CONST0:
						// register combiner, const 0
						// the index relates to the texture stage; store this as (stage * 2) + 0
						regCombinerPhysicalIndex = logicalIndex * 2;
						isRegisterCombiner = true;
						break;
					case CG_COMBINER_STAGE_CONST1:
						// register combiner, const 1
						// the index relates to the texture stage; store this as (stage * 2) + 1
						regCombinerPhysicalIndex = (logicalIndex * 2) + 1;
						isRegisterCombiner = true;
						break;
					default:
						// normal constant
						break;
					}

					// Trim the '[0]' suffix if it exists, we will add our own indexing later
					if (StringUtil::endsWith(paramName, "[0]", false))
					{
						paramName.erase(paramName.size() - 3);
					}


					GpuConstantDefinition def;
					def.arraySize = contextArraySize;
					mapTypeAndElementSize(paramType, isRegisterCombiner, def);

					if (def.constType == GCT_UNKNOWN)
					{
						LogManager::getSingleton().logMessage(
							"Problem parsing the following Cg Uniform: '"
							+ paramName + "' in file " + mName);
						// next uniform
						parameter = cgGetNextParameter(parameter);
						continue;
					}
					if (isRegisterCombiner)
					{
						def.physicalIndex = regCombinerPhysicalIndex;
					}
					else
					{
						// base position on existing buffer contents
						if (def.isFloat())
						{
							def.physicalIndex = mFloatLogicalToPhysical->bufferSize;
						}
						else
						{
							def.physicalIndex = mIntLogicalToPhysical->bufferSize;
						}
					}

					def.logicalIndex = logicalIndex;
					if( mParametersMap.find(paramName) == mParametersMap.end())
					{
						mParametersMap.insert(GpuConstantDefinitionMap::value_type(paramName, def));
						mParametersMapSizeAsBuffer += sizeof(size_t);
						mParametersMapSizeAsBuffer += paramName.size();
						mParametersMapSizeAsBuffer += sizeof(GpuConstantDefinition);
					}

					// Record logical / physical mapping
					if (def.isFloat())
					{
											OGRE_LOCK_MUTEX(mFloatLogicalToPhysical->mutex);
						mFloatLogicalToPhysical->map.insert(
							GpuLogicalIndexUseMap::value_type(def.logicalIndex, 
								GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
						mFloatLogicalToPhysical->bufferSize += def.arraySize * def.elementSize;
					}
					else
					{
											OGRE_LOCK_MUTEX(mIntLogicalToPhysical->mutex);
						mIntLogicalToPhysical->map.insert(
							GpuLogicalIndexUseMap::value_type(def.logicalIndex, 
								GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
						mIntLogicalToPhysical->bufferSize += def.arraySize * def.elementSize;
					}

					break;
				}                   
			}

			// now handle uniform samplers. This is needed to fix their register positions
			// if delegating to a GLSL shader.
			if (mDelegate && cgGetParameterVariability(parameter) == CG_UNIFORM && (
				paramType == CG_SAMPLER1D ||
				paramType == CG_SAMPLER2D ||
				paramType == CG_SAMPLER3D ||
				paramType == CG_SAMPLERCUBE ||
				paramType == CG_SAMPLERRECT) &&
				cgGetParameterDirection(parameter) != CG_OUT && 
				cgIsParameterReferenced(parameter))
			{
				String paramName = cgGetParameterName(parameter);
				CGresource res = cgGetParameterResource(parameter);
				int pos = -1;
				switch (res)
				{
				case CG_TEXUNIT0: pos = 0; break;
				case CG_TEXUNIT1: pos = 1; break;
				case CG_TEXUNIT2: pos = 2; break;
				case CG_TEXUNIT3: pos = 3; break;
				case CG_TEXUNIT4: pos = 4; break;
				case CG_TEXUNIT5: pos = 5; break;
				case CG_TEXUNIT6: pos = 6; break;
				case CG_TEXUNIT7: pos = 7; break;
				case CG_TEXUNIT8: pos = 8; break;
				case CG_TEXUNIT9: pos = 9; break;
				case CG_TEXUNIT10: pos = 10; break;
				case CG_TEXUNIT11: pos = 11; break;
				case CG_TEXUNIT12: pos = 12; break;
				case CG_TEXUNIT13: pos = 13; break;
				case CG_TEXUNIT14: pos = 14; break;
				case CG_TEXUNIT15: pos = 15; break;
#if(CG_VERSION_NUM >= 3000)
				case CG_TEXUNIT16: pos = 16; break;
				case CG_TEXUNIT17: pos = 17; break;
				case CG_TEXUNIT18: pos = 18; break;
				case CG_TEXUNIT19: pos = 19; break;
				case CG_TEXUNIT20: pos = 20; break;
				case CG_TEXUNIT21: pos = 21; break;
				case CG_TEXUNIT22: pos = 22; break;
				case CG_TEXUNIT23: pos = 23; break;
				case CG_TEXUNIT24: pos = 24; break;
				case CG_TEXUNIT25: pos = 25; break;
				case CG_TEXUNIT26: pos = 26; break;
				case CG_TEXUNIT27: pos = 27; break;
				case CG_TEXUNIT28: pos = 28; break;
				case CG_TEXUNIT29: pos = 29; break;
				case CG_TEXUNIT30: pos = 30; break;
				case CG_TEXUNIT31: pos = 31; break;
#endif
				default:
					break;
				}
				if (pos != -1)
				{
					mSamplerRegisterMap.insert(std::make_pair(paramName, pos));
				}
			}

			// Get next
			parameter = cgGetNextParameter(parameter);
		}

		
	}
Exemplo n.º 4
0
	//---------------------------------------------------------------------
	void CgProgram::recurseParams(CGparameter parameter, size_t contextArraySize) const
	{
		while (parameter != 0)
        {
            // Look for uniform (non-sampler) parameters only
            // Don't bother enumerating unused parameters, especially since they will
            // be optimised out and therefore not in the indexed versions
            CGtype paramType = cgGetParameterType(parameter);

            if (cgGetParameterVariability(parameter) == CG_UNIFORM &&
                paramType != CG_SAMPLER1D &&
                paramType != CG_SAMPLER2D &&
                paramType != CG_SAMPLER3D &&
                paramType != CG_SAMPLERCUBE &&
                paramType != CG_SAMPLERRECT &&
                cgGetParameterDirection(parameter) != CG_OUT && 
                cgIsParameterReferenced(parameter))
            {
				int arraySize;

				switch(paramType)
				{
				case CG_STRUCT:
					recurseParams(cgGetFirstStructParameter(parameter));
					break;
				case CG_ARRAY:
					// Support only 1-dimensional arrays
					arraySize = cgGetArraySize(parameter, 0);
					recurseParams(cgGetArrayParameter(parameter, 0), (size_t)arraySize);
					break;
				default:
					// Normal path (leaf)
					String paramName = cgGetParameterName(parameter);
					size_t logicalIndex = cgGetParameterResourceIndex(parameter);

					// Get the parameter resource, to calculate the physical index
					CGresource res = cgGetParameterResource(parameter);
					bool isRegisterCombiner = false;
					size_t regCombinerPhysicalIndex = 0;
					switch (res)
					{
					case CG_COMBINER_STAGE_CONST0:
						// register combiner, const 0
						// the index relates to the texture stage; store this as (stage * 2) + 0
						regCombinerPhysicalIndex = logicalIndex * 2;
						isRegisterCombiner = true;
						break;
					case CG_COMBINER_STAGE_CONST1:
						// register combiner, const 1
						// the index relates to the texture stage; store this as (stage * 2) + 1
						regCombinerPhysicalIndex = (logicalIndex * 2) + 1;
						isRegisterCombiner = true;
						break;
					default:
						// normal constant
						break;
					}

					// Trim the '[0]' suffix if it exists, we will add our own indexing later
					if (StringUtil::endsWith(paramName, "[0]", false))
					{
						paramName.erase(paramName.size() - 3);
					}


					GpuConstantDefinition def;
					def.arraySize = contextArraySize;
					mapTypeAndElementSize(paramType, isRegisterCombiner, def);

					if (def.constType == GCT_UNKNOWN)
					{
						LogManager::getSingleton().logMessage(
							"Problem parsing the following Cg Uniform: '"
							+ paramName + "' in file " + mName);
						// next uniform
						continue;
					}
					if (isRegisterCombiner)
					{
						def.physicalIndex = regCombinerPhysicalIndex;
					}
					else
					{
						// base position on existing buffer contents
						if (def.isFloat())
						{
							def.physicalIndex = mFloatLogicalToPhysical.bufferSize;
						}
						else
						{
							def.physicalIndex = mIntLogicalToPhysical.bufferSize;
						}
					}


					mConstantDefs.map.insert(GpuConstantDefinitionMap::value_type(paramName, def));

					// Record logical / physical mapping
					if (def.isFloat())
					{
						OGRE_LOCK_MUTEX(mFloatLogicalToPhysical.mutex)
						mFloatLogicalToPhysical.map.insert(
							GpuLogicalIndexUseMap::value_type(logicalIndex, 
								GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize)));
						mFloatLogicalToPhysical.bufferSize += def.arraySize * def.elementSize;
						mConstantDefs.floatBufferSize = mFloatLogicalToPhysical.bufferSize;
					}
					else
					{
						OGRE_LOCK_MUTEX(mIntLogicalToPhysical.mutex)
						mIntLogicalToPhysical.map.insert(
							GpuLogicalIndexUseMap::value_type(logicalIndex, 
								GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize)));
						mIntLogicalToPhysical.bufferSize += def.arraySize * def.elementSize;
						mConstantDefs.intBufferSize = mIntLogicalToPhysical.bufferSize;
					}

					// Deal with array indexing
					mConstantDefs.generateConstantDefinitionArrayEntries(paramName, def);

					break;
		
				}
					
            }
            // Get next
            parameter = cgGetNextParameter(parameter);
        }

        
    }