Ejemplo n.º 1
0
void
CStarField::CreateVBResult( unsigned long in_VBHandle )
{
	if(!m_bPreloadError)
	{
		if(in_VBHandle>0)
		{
			m_VBHandle = in_VBHandle;
			// decode texture
			std::string TextureName("/data/");
			TextureName += "star.jpg";
			CLoadProxy::getInstance().DecodeImageFromFile( TextureName.c_str(), this );
		}
		else
		{
			m_bPreloadError = true;
			PreloadComplete(false);
		}
	}
}
Ejemplo n.º 2
0
static void TranslateVariableName(HLSLCrossCompilerContext* psContext, const Operand* psOperand, uint32_t ui32TOFlag, uint32_t* pui32IgnoreSwizzle)
{
    int integerConstructor = 0;
    bstring glsl = *psContext->currentGLSLString;

    *pui32IgnoreSwizzle = 0;

    if(psOperand->eType != OPERAND_TYPE_IMMEDIATE32 &&
        psOperand->eType != OPERAND_TYPE_IMMEDIATE64 &&
        psOperand->eType != OPERAND_TYPE_CONSTANT_BUFFER)
    {
		const uint32_t swizCount = psOperand->iNumComponents;
		SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand);

		if( (ui32TOFlag & (TO_FLAG_INTEGER|TO_FLAG_UNSIGNED_INTEGER)) == (TO_FLAG_INTEGER|TO_FLAG_UNSIGNED_INTEGER))
		{
			//Can be either int or uint
			if(eType != SVT_INT && eType != SVT_UINT)
			{
				if(swizCount == 1)
					bformata(glsl, "int(");
				else
					bformata(glsl, "ivec%d(", swizCount);

				integerConstructor = 1;
			}
		}
		else
		{
			if((ui32TOFlag & (TO_FLAG_INTEGER|TO_FLAG_DESTINATION))==TO_FLAG_INTEGER &&
				eType != SVT_INT)
			{
				//Convert to int
				if(swizCount == 1)
					bformata(glsl, "int(");
				else
					bformata(glsl, "ivec%d(", swizCount);

				integerConstructor = 1;
			}
			if((ui32TOFlag & (TO_FLAG_UNSIGNED_INTEGER|TO_FLAG_DESTINATION))==TO_FLAG_UNSIGNED_INTEGER &&
				eType != SVT_UINT)
			{
				//Convert to uint
				if(swizCount == 1)
					bformata(glsl, "uint(");
				else
					bformata(glsl, "uvec%d(", swizCount);
				integerConstructor = 1;
			}
		}
    }

    switch(psOperand->eType)
    {
        case OPERAND_TYPE_IMMEDIATE32:
        {
            if(psOperand->iNumComponents == 1)
            {
				if(ui32TOFlag & TO_FLAG_UNSIGNED_INTEGER)
				{
					bformata(glsl, "%uu",
						*((unsigned int*)(&psOperand->afImmediates[0])));
				}
                else
				if((ui32TOFlag & TO_FLAG_INTEGER) || psOperand->iIntegerImmediate || fpcheck(psOperand->afImmediates[0]))
				{
					bformata(glsl, "%d",
						*((int*)(&psOperand->afImmediates[0])));
				}
				else
				{
					bformata(glsl, "%f",
                    psOperand->afImmediates[0]);
				}
            }
            else
            {
                if(ui32TOFlag & TO_FLAG_UNSIGNED_INTEGER)
                {
                    bformata(glsl, "uvec4(%uu, %uu, %uu, %uu)",
                        *(unsigned int*)&psOperand->afImmediates[0],
                        *(unsigned int*)&psOperand->afImmediates[1],
                        *(unsigned int*)&psOperand->afImmediates[2],
                        *(unsigned int*)&psOperand->afImmediates[3]);
                }
                else
                if((ui32TOFlag & TO_FLAG_INTEGER) ||
                    psOperand->iIntegerImmediate ||
                    fpcheck(psOperand->afImmediates[0]) ||
                    fpcheck(psOperand->afImmediates[1]) ||
                    fpcheck(psOperand->afImmediates[2]) ||
                    fpcheck(psOperand->afImmediates[3]))
                {
                    bformata(glsl, "ivec4(%d, %d, %d, %d)",
                        *(int*)&psOperand->afImmediates[0],
                        *(int*)&psOperand->afImmediates[1],
                        *(int*)&psOperand->afImmediates[2],
                        *(int*)&psOperand->afImmediates[3]);
                }
                else
                {
                    bformata(glsl, "vec4(%f, %f, %f, %f)",
                        psOperand->afImmediates[0],
                        psOperand->afImmediates[1],
                        psOperand->afImmediates[2],
                        psOperand->afImmediates[3]);
                }
                if(psOperand->iNumComponents != 4)
                {
                    AddSwizzleUsingElementCount(psContext, psOperand->iNumComponents);
                }
            }
            break;
        }
        case OPERAND_TYPE_IMMEDIATE64:
        {
            if(psOperand->iNumComponents == 1)
            {
                bformata(glsl, "%f",
                    psOperand->adImmediates[0]);
            }
            else
            {
                bformata(glsl, "dvec4(%f, %f, %f, %f)",
                    psOperand->adImmediates[0],
                    psOperand->adImmediates[1],
                    psOperand->adImmediates[2],
                    psOperand->adImmediates[3]);
                if(psOperand->iNumComponents != 4)
                {
                    AddSwizzleUsingElementCount(psContext, psOperand->iNumComponents);
                }
            }
            break;
        }
        case OPERAND_TYPE_INPUT:
        {
            switch(psOperand->iIndexDims)
            {
                case INDEX_2D:
                {
                    if(psOperand->aui32ArraySizes[1] == 0)//Input index zero - position.
                    {
                        bcatcstr(glsl, "gl_in");
                        TranslateOperandIndex(psContext, psOperand, TO_FLAG_NONE);//Vertex index
                        bcatcstr(glsl, ".gl_Position");
                    }
                    else
                    {
                        const char* name = "Input";
                        if(ui32TOFlag & TO_FLAG_DECLARATION_NAME)
                        {
                            name = GetDeclaredInputName(psContext, psContext->psShader->eShaderType, psOperand);
                        }
                        
                        bformata(glsl, "%s%d", name, psOperand->aui32ArraySizes[1]);
                        TranslateOperandIndex(psContext, psOperand, TO_FLAG_NONE);//Vertex index
                    }
                    break;
                }
                default:
                {
                    if(psOperand->eIndexRep[0] == OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE)
                    {
                        bformata(glsl, "Input%d[int(", psOperand->ui32RegisterNumber);
                        TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE);
                        bcatcstr(glsl, ")]");
                    }
                    else
                    {
                        if(psContext->psShader->aIndexedInput[psOperand->ui32RegisterNumber] != 0)
                        {
                            const uint32_t parentIndex = psContext->psShader->aIndexedInputParents[psOperand->ui32RegisterNumber];
                            bformata(glsl, "Input%d[%d]", parentIndex,
                                psOperand->ui32RegisterNumber - parentIndex);
                        }
                        else
                        {
                            if(ui32TOFlag & TO_FLAG_DECLARATION_NAME)
                            {
                                const char* name = GetDeclaredInputName(psContext, psContext->psShader->eShaderType, psOperand);
								bcatcstr(glsl, name);
                            }
							else
							{
								bformata(glsl, "Input%d", psOperand->ui32RegisterNumber);
							}
                        }
                    }
                    break;
                }
            }
            break;
        }
        case OPERAND_TYPE_OUTPUT:
        {
            bformata(glsl, "Output%d", psOperand->ui32RegisterNumber);
            if(psOperand->psSubOperand[0])
            {
                bcatcstr(glsl, "[int("); //Indexes must be integral.
                TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE);
                bcatcstr(glsl, ")]");
            }
            break;
        }
        case OPERAND_TYPE_OUTPUT_DEPTH:
        case OPERAND_TYPE_OUTPUT_DEPTH_GREATER_EQUAL:
        case OPERAND_TYPE_OUTPUT_DEPTH_LESS_EQUAL:
        {
            bcatcstr(glsl, "gl_FragDepth");
            break;
        }
        case OPERAND_TYPE_TEMP:
        {
			SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand);
            bcatcstr(glsl, "Temp");

            if(eType == SVT_INT)
            {
                bcatcstr(glsl, "_int");
            }
            else if(eType == SVT_UINT)
            {
                bcatcstr(glsl, "_uint");
            }
			else if(eType == SVT_DOUBLE)
			{
				bcatcstr(glsl, "_double");
			}
            else if(eType == SVT_VOID ||
                (ui32TOFlag & TO_FLAG_DESTINATION))
            {
                if(ui32TOFlag & TO_FLAG_INTEGER)
                {
                    bcatcstr(glsl, "_int");
                }
                else
                if(ui32TOFlag & TO_FLAG_UNSIGNED_INTEGER)
                {
                    bcatcstr(glsl, "_uint");
                }
            }

			bformata(glsl, "[%d]", psOperand->ui32RegisterNumber);

            break;
        }
		case OPERAND_TYPE_SPECIAL_IMMCONSTINT:
		{
            bformata(glsl, "IntImmConst%d", psOperand->ui32RegisterNumber);
            break;
		}
        case OPERAND_TYPE_SPECIAL_IMMCONST:
        {
            bformata(glsl, "ImmConst%d", psOperand->ui32RegisterNumber);
            break;
        }
        case OPERAND_TYPE_SPECIAL_OUTBASECOLOUR:
        {
            bcatcstr(glsl, "BaseColour");
            break;
        }
        case OPERAND_TYPE_SPECIAL_OUTOFFSETCOLOUR:
        {
            bcatcstr(glsl, "OffsetColour");
            break;
        }
        case OPERAND_TYPE_SPECIAL_POSITION:
        {
            bcatcstr(glsl, "gl_Position");
            break;
        }
        case OPERAND_TYPE_SPECIAL_FOG:
        {
            bcatcstr(glsl, "Fog");
            break;
        }
        case OPERAND_TYPE_SPECIAL_POINTSIZE:
        {
            bcatcstr(glsl, "gl_PointSize");
            break;
        }
        case OPERAND_TYPE_SPECIAL_ADDRESS:
        {
            bcatcstr(glsl, "Address");
            break;
        }
		case OPERAND_TYPE_SPECIAL_TEXCOORD:
		{
			bformata(glsl, "TexCoord%d", psOperand->ui32RegisterNumber);
			break;
		}
        case OPERAND_TYPE_CONSTANT_BUFFER:
        {
            const char* StageName = "VS";
            ConstantBuffer* psCBuf = NULL;
            ShaderVarType* psVarType = NULL;
            int32_t index = -1;
            GetConstantBufferFromBindingPoint(RGROUP_CBUFFER, psOperand->aui32ArraySizes[0], &psContext->psShader->sInfo, &psCBuf);

            switch(psContext->psShader->eShaderType)
            {
                case PIXEL_SHADER:
                {
                    StageName = "PS";
                    break;
                }
                case HULL_SHADER:
                {
                    StageName = "HS";
                    break;
                }
                case DOMAIN_SHADER:
                {
                    StageName = "DS";
                    break;
                }
                case GEOMETRY_SHADER:
                {
                    StageName = "GS";
                    break;
                }
				case COMPUTE_SHADER:
				{
					StageName = "CS";
					break;
				}
                default:
                {
                    break;
                }
            }

            if(ui32TOFlag & TO_FLAG_DECLARATION_NAME)
            {
                pui32IgnoreSwizzle[0] = 1;
            }

			if((psContext->flags & HLSLCC_FLAG_UNIFORM_BUFFER_OBJECT)!=HLSLCC_FLAG_UNIFORM_BUFFER_OBJECT)
			{
				if(psCBuf)
				{
					//$Globals.
					if(psCBuf->Name[0] == '$')
					{
						bformata(glsl, "Globals%s", StageName);
					}
					else
					{
						bformata(glsl, "%s%s", psCBuf->Name, StageName);
					}
					if((ui32TOFlag & TO_FLAG_DECLARATION_NAME) != TO_FLAG_DECLARATION_NAME)
					{
						bcatcstr(glsl, ".");
					}
				}
				else
				{
					//bformata(glsl, "cb%d", psOperand->aui32ArraySizes[0]);
				}
		}

            if((ui32TOFlag & TO_FLAG_DECLARATION_NAME) != TO_FLAG_DECLARATION_NAME)
            {
                //Work out the variable name. Don't apply swizzle to that variable yet.
				int32_t rebase = 0;

				if(psCBuf)
				{
					GetShaderVarFromOffset(psOperand->aui32ArraySizes[1], psOperand->aui32Swizzle, psCBuf, &psVarType, &index, &rebase);

					bformata(glsl, "%s", psVarType->FullName);
				}
				else // We don't have a semantic for this variable, so try the raw dump appoach.
				{
					bformata(glsl, "cb%d.data", psOperand->aui32ArraySizes[0]);//
					index = psOperand->aui32ArraySizes[1];
				}

				//Dx9 only?
				if(psOperand->psSubOperand[0] != NULL)
				{
					SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand->psSubOperand[0]);
					if(eType != SVT_INT && eType != SVT_UINT)
					{
						bcatcstr(glsl, "[int("); //Indexes must be integral.
						TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE);
						bcatcstr(glsl, ")]");
					}
					else
					{
						bcatcstr(glsl, "["); //Indexes must be integral.
						TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE);
						bcatcstr(glsl, "]");
					}

					ASSERT(index == 0 || index == -1);
				}
				else
				if(index != -1 && psOperand->psSubOperand[1] != NULL)
				{
					//Array of matrices is treated as array of vec4s
					if(index != -1)
					{
						SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand->psSubOperand[1]);
						if(eType != SVT_INT && eType != SVT_UINT)
						{
							bcatcstr(glsl, "[int(");
							TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE);
							bformata(glsl, ") + %d]", index);
						}
						else
						{
							bcatcstr(glsl, "[");
							TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE);
							bformata(glsl, " + %d]", index);
						}
					}
				}
				else if(index != -1)
                {
                    bformata(glsl, "[%d]", index);
                }
				else if(psOperand->psSubOperand[1] != NULL)
				{
					SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand->psSubOperand[1]);
					if(eType != SVT_INT && eType != SVT_UINT)
					{
						bcatcstr(glsl, "[");
						TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE);
						bcatcstr(glsl, "]");
					}
					else
					{
						bcatcstr(glsl, "[int(");
						TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE);
						bcatcstr(glsl, ")]");
					}
				}

				if(psVarType && psVarType->Class == SVC_VECTOR)
				{
					switch(rebase)
					{
						case 4:
						{
							if(psVarType->Columns == 2)
							{
								//.x(GLSL) is .y(HLSL). .y(GLSL) is .z(HLSL)
								bcatcstr(glsl, ".xxyx");
							}
							else if(psVarType->Columns == 3)
							{
								//.x(GLSL) is .y(HLSL). .y(GLSL) is .z(HLSL) .z(GLSL) is .w(HLSL)
								bcatcstr(glsl, ".xxyz");
							}
							break;
						}
						case 8:
						{
							if(psVarType->Columns == 2)
							{
								//.x(GLSL) is .z(HLSL). .y(GLSL) is .w(HLSL)
								bcatcstr(glsl, ".xxxy");
							}
							break;
						}
						case 0:
						default:
						{
							//No rebase, but extend to vec4.
							if(psVarType->Columns == 2)
							{
								bcatcstr(glsl, ".xyxx");
							}
							else if(psVarType->Columns == 3)
							{
								bcatcstr(glsl, ".xyzx");
							}
							break;
						}

					}
				}

				if(psVarType && psVarType->Class == SVC_SCALAR)
				{
					*pui32IgnoreSwizzle = 1;
				}
            }
            break;
        }
        case OPERAND_TYPE_RESOURCE:
        {
            TextureName(psContext, psOperand->ui32RegisterNumber, 0);
			*pui32IgnoreSwizzle = 1;
            break;
        }
        case OPERAND_TYPE_SAMPLER:
        {
            bformata(glsl, "Sampler%d", psOperand->ui32RegisterNumber);
			*pui32IgnoreSwizzle = 1;
            break;
        }
        case OPERAND_TYPE_FUNCTION_BODY:
        {
            const uint32_t ui32FuncBody = psOperand->ui32RegisterNumber;
            const uint32_t ui32FuncTable = psContext->psShader->aui32FuncBodyToFuncTable[ui32FuncBody];
            //const uint32_t ui32FuncPointer = psContext->psShader->aui32FuncTableToFuncPointer[ui32FuncTable];
            const uint32_t ui32ClassType = psContext->psShader->sInfo.aui32TableIDToTypeID[ui32FuncTable];
            const char* ClassTypeName = &psContext->psShader->sInfo.psClassTypes[ui32ClassType].Name[0];
            const uint32_t ui32UniqueClassFuncIndex = psContext->psShader->ui32NextClassFuncName[ui32ClassType]++;

            bformata(glsl, "%s_Func%d", ClassTypeName, ui32UniqueClassFuncIndex);
            break;
        }
		case OPERAND_TYPE_INPUT_FORK_INSTANCE_ID:
		{
			bcatcstr(glsl, "forkInstanceID");
            *pui32IgnoreSwizzle = 1;
			return;
		}
		case OPERAND_TYPE_IMMEDIATE_CONSTANT_BUFFER:
		{
            bcatcstr(glsl, "immediateConstBufferF");

            if(psOperand->psSubOperand[0])
            {
                bcatcstr(glsl, "(int("); //Indexes must be integral.
                TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE);
                bcatcstr(glsl, "))");
            }
			break;
		}
		case OPERAND_TYPE_INPUT_DOMAIN_POINT:
		{
			bcatcstr(glsl, "gl_TessCoord");
			break;
		}
		case OPERAND_TYPE_INPUT_CONTROL_POINT:
		{
			if(psOperand->aui32ArraySizes[1] == 0)//Input index zero - position.
			{
				bformata(glsl, "gl_in[%d].gl_Position", psOperand->aui32ArraySizes[0]);
			}
            else
            {
                bformata(glsl, "Input%d[%d]", psOperand->aui32ArraySizes[1], psOperand->aui32ArraySizes[0]);
            }
            break;
		}
		case OPERAND_TYPE_NULL:
		{
			// Null register, used to discard results of operations
			bcatcstr(glsl, "//null");
			break;
		}
        case OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID:
        {
            bcatcstr(glsl, "gl_InvocationID");
            *pui32IgnoreSwizzle = 1;
            break;
        }
        case OPERAND_TYPE_OUTPUT_COVERAGE_MASK:
        {
            bcatcstr(glsl, "gl_SampleMask[0]");
            *pui32IgnoreSwizzle = 1;
            break;
        }
        case OPERAND_TYPE_INPUT_COVERAGE_MASK:
        {
            bcatcstr(glsl, "gl_SampleMaskIn[0]");
            //Skip swizzle on scalar types.
            *pui32IgnoreSwizzle = 1;
            break;
        }
		case OPERAND_TYPE_INPUT_THREAD_ID://SV_DispatchThreadID
		{
			bcatcstr(glsl, "gl_GlobalInvocationID");
			break;
		}
		case OPERAND_TYPE_INPUT_THREAD_GROUP_ID://SV_GroupThreadID
		{
			bcatcstr(glsl, "gl_LocalInvocationID");
			break;
		}
		case OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP://SV_GroupID
		{
			bcatcstr(glsl, "gl_WorkGroupID");
			break;
		}
		case OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP_FLATTENED://SV_GroupIndex
		{
			bcatcstr(glsl, "gl_LocalInvocationIndex");
			break;
		}
        case OPERAND_TYPE_UNORDERED_ACCESS_VIEW:
        {
            bformata(glsl, "UAV%d", psOperand->ui32RegisterNumber);
            break;
        }
        case OPERAND_TYPE_THREAD_GROUP_SHARED_MEMORY:
        {
            bformata(glsl, "TGSM%d", psOperand->ui32RegisterNumber);
            break;
        }
		case OPERAND_TYPE_INPUT_PRIMITIVEID:
		{
			bcatcstr(glsl, "gl_PrimitiveID");
			break;
		}
        case OPERAND_TYPE_INDEXABLE_TEMP:
        {
            bformata(glsl, "TempArray%d", psOperand->aui32ArraySizes[0]);
            bformata(glsl, "[%d", psOperand->aui32ArraySizes[1]);
            
            if(psOperand->psSubOperand[1])
            {
                bcatcstr(glsl, "+");
                TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE);
                
            }
            bcatcstr(glsl, "]");
            break;
        }
		case OPERAND_TYPE_STREAM:
		{
			bformata(glsl, "%d", psOperand->ui32RegisterNumber);
			break;
		}
		case OPERAND_TYPE_INPUT_GS_INSTANCE_ID:
		{
			bcatcstr(glsl, "gl_InvocationID");
			break;
		}
		case OPERAND_TYPE_THIS_POINTER:
		{
			/*
				The "this" register is a register that provides up to 4 pieces of information:
				X: Which CB holds the instance data
				Y: Base element offset of the instance data within the instance CB
				Z: Base sampler index
				W: Base Texture index

				Can be different for each function call
			*/
			break;
		}
        default:
        {
            ASSERT(0);
            break;
        }
    }

    if(integerConstructor)
    {
        bcatcstr(glsl, ")");
    }
}
//--
//
// Analysis
//
//--
bool MultiresolutionMesh::Analysis(int)
{
	//
	// HSI
	//
	#ifdef _HSI_COLOR_
		std::vector<Vector3d> color_backup;
		if( ColorNumber() == VertexNumber() )
		{
			color_backup = colors;
			Vector3d tmp_hsi;
			for( int i=0; i<ColorNumber(); i++ )
			{
				Rgb2Hsi( Color(i), tmp_hsi );
				Color(i) = tmp_hsi;
			}
		}
	#endif


	//
	// RGB 2 Grayscale
	//
	#ifdef _RGB_2_GRAYSCALE_
		std::vector<Vector3d> color_backup;
		if( ColorNumber() == VertexNumber() ) {
			color_backup = colors;
			double tmp;
			for( int i=0; i<ColorNumber(); i++ ) {
				tmp = (Color(i)[0]+Color(i)[1]+Color(i)[2])/3.0;
				Color(i) = tmp;
			}
		}
	#endif
	

	// Initialize member variables
	current_level_number = -1;
	levels.clear();
	mean_curvature.clear();
	gaussian_curvature.clear();
	texture.Reset();
	vertex_map.clear();
	edge_list.clear();

	// Initialize normals
	ComputeFaceNormals();
	ComputeVertexNormals();

	// Initialize progressive decomposition
	BeginProgressiveDecomposition();
	

	// Initialize curvatures
//	mean_curvature.resize( VertexNumber() );
//	gaussian_curvature.resize( VertexNumber() );
//	ComputeMeanCurvature();
//	ComputeGaussianCurvature();

	// Initialize texture
	if( (TextureNumber()!=0) && (TextureName().empty()==false) ) {
		use_texture = texture.ReadFile( TextureName() );
	}
	else {
		use_texture = false;
	}

	if( ColorNumber() == VertexNumber() ) {
		use_color = true;
	}
	else {
		use_color = false;
	}

	use_normal = true;

//	permutation.assign(VertexNumber(), -1);
//	map.assign( VertexNumber(), -1);

	// Initialize vertex map
	// This table indicates on which vertex each vertex has collapsed to
	vertex_map.resize( VertexNumber() );
	for( int i=0; i<VertexNumber(); i++ ) {
		vertex_map[i] = i;
	}

//	std::cout<<bounding_box.Center()<<std::endl;
//	for( int i=0; i<VertexNumber(); i++ ) {
//		if( 
//			Vertex(i)[0]>-0.01 &&
//			Vertex(i)[2]>-0.01 &&
//			Vertex(i)[0]<0.01 &&
//			Vertex(i)[2]<0.01
//		)
//			std::cout<<i<<" "<<Vertex(i)<<std::endl;
//	}


	ConstPairContractionIterator itp;
	ConstNeighborIterator itn;
	std::vector<bool> locked_vertices(VertexNumber(), false);
//	levels.resize( base_level );
	current_level_number = 0;

	//-- Test --
	//int vnum = VertexNumber();
	//timer.Reset();
	//timer.Start();
	//test<<current_level_number<<'\t'<<vnum<<endl;
	//--

	#ifdef _OUTPUT_LEVELS_
		// Write the finest level statistics
		std::ofstream level_stats_file("levels.log");
		level_stats_file<<current_level_number<<'\t'<<ValidVertexNumber()<<'\t'<<ValidFaceNumber()<<std::endl;
		// Write the finest level mesh (the initial mesh)
		char filename[255];
		sprintf(filename,"level%02d.wrl",current_level_number);
		WriteFile(filename);
	#endif


	//
	// Create the levels of details
	//
	while( 1 ) {

		// Add a new level
		levels.push_back( ResolutionLevel() );

		//
		// Select the odd vertices
		// Remove the odd vertices
		// Lock the neighbors
		// Odd vertices are a set of independent vertices selected to be removed
		// The remaining (even) vertices compose the next coarse level
		//
		while( !pair_contractions.empty() ) {

			#ifdef _TEST_PLAN_
				// Lock one predefined vertex
				locked_vertices[1844] = true; // plan-20000
			//	locked_vertices[30670] = true; // plan-irregulier
			#endif

			// Get pair contraction with minimum cost
			PairContraction pair = MinimumPairContraction();
			
			// Remove pair from contraction candidate list
			RemovePairContraction( pair.Candidate() );
			
			// Do not perform bad contraction until the level of detail is very low
			if( pair.Cost()>=invalid_contraction_penalty && current_level_number<15 ) {
				locked_vertices[pair.Candidate()] = true;
				continue;
			}
			
			// Test to see if after the pair contraction,
			// one vertex has a valence of 3
			// This case screws up the predict operator
			itn = NeighborVertices(pair.Candidate()).begin();
			while( itn != NeighborVertices(pair.Candidate()).end() ) {
				if(current_level_number<15 && NeighborVertexNumber(*itn)==4 && FindNeighborVertex(pair.Target(), *itn) && !IsBorderVertex(*itn)) {
					locked_vertices[pair.Candidate()] = true;
				}
				++itn;
			}
			
			if( locked_vertices[pair.Candidate()] ) continue;
			
			// Map the candidate to the target
			vertex_map[pair.Candidate()] = pair.Target();
			
			// Lock the selected vertex and its neighbors
		//	valid_vertices[pair.Candidate()] = false;
		
			// Collapse candidate
			Collapse( pair );
			
			// Update normals
			UpdateNormals( pair );
			
			// Update quadrics
			UpdateQuadrics(pair);
			
			// Update pair cost of surronding vertices
			if( pair.Target() >= 0 ) {	
				// Compute neighbor vertex cost
				itn = NeighborVertices(pair.Target()).begin();
				while( itn != NeighborVertices(pair.Target()).end() ) {
					if( locked_vertices[*itn] == false ) {
						RemovePairContraction(*itn);
						ComputeEdgeCostAtVertex(*itn);
					}
					++itn;
				}
			}
			
			// Lock the neighborhood of the selected vertex
			itn = NeighborVertices(pair.Candidate()).begin();
			while( itn != NeighborVertices(pair.Candidate()).end() ) {
				// If the vertex is not already locked
				if( locked_vertices[*itn] == false ) {
					// Remove its pair contraction estimation
					RemovePairContraction(*itn);
					// Lock it
					locked_vertices[*itn] = true;
				}
				// Next neighbor
				++itn;
			}
			
			// Record pair contraction
			levels[current_level_number].pair_contractions.push_front( pair );
		}

//		bool left = false;
		int left_vertex_number = 0;

		// Compute new edge collapse pairs
		for(int i=0; i<VertexNumber(); i++ ) {
			if( valid_vertices[i] == false ) continue;
			left_vertex_number++;
			locked_vertices[i] = false;
			ComputeEdgeCostAtVertex(i);
		}

		// Update the normals
		ComputeProgressiveFaceNormals();
		ComputeProgressiveVertexNormals();

		// Next level
		current_level_number++;
		
		#ifdef _OUTPUT_LEVELS_
			// Write the current level statistics
			level_stats_file<<current_level_number<<'\t'<<ValidVertexNumber()<<'\t'<<ValidFaceNumber()<<std::endl;
			// Write the current level mesh
			sprintf(filename,"level%02d.wrl",current_level_number);
			WriteFile(filename);
		#endif

		// Is there any vertex left ?
//		if( left_vertex_number == 0 ) break;
	//	if( left_vertex_number < 100 ) break;
		if( left_vertex_number < 10 ) break;

	}

	// End the progressive decomposition
	EndProgressiveDecomposition();

	// Reset
	locked_vertices.clear();


	//
	// Go back the initial fine level
	// Reconstruct all levels
	//
	while( current_level_number > 0 ) {
		// Next level
		current_level_number--;

		// Insert odd vertices
		itp = levels[current_level_number].pair_contractions.begin();
		while( itp != levels[current_level_number].pair_contractions.end() ) {
			// Expand candidate of current pair contraction
			Expand( *itp );
			// Next pair contraction
			++itp;
		}
	}

	// Update the normals
	ComputeProgressiveFaceNormals();
	ComputeProgressiveVertexNormals();

	for( int i=0; i<(int)levels.size(); i++ ) {
		levels[i].Resize( VertexNumber() );
	}

	//
	// Compute the details
	//
	while( current_level_number < LevelNumber() ) {

		//
		// Predict odd vertices
		//
		itp = levels[current_level_number].pair_contractions.begin();
		while( itp != levels[current_level_number].pair_contractions.end() ) {
			// Compute wavelet coefficient
			PredictVertex( itp->Candidate(), ODD_VERTEX );
			
			// Invalidate the odd vertex
			valid_vertices[itp->Candidate()] = false;

			#ifdef _TEST_PLAN_
				// Null the details
				levels[current_level_number].geometric_details[itp->Candidate()] = 0;
			#endif
			
			// Next pair contraction
			++itp;
		}


		//
		// Predict even vertices
		//
		for(int i=0; i<VertexNumber(); i++ ) {
			// Valid vertex ?
			if( valid_vertices[i] == false ) continue;
			
			// Predict even vertex
			PredictVertex( i, EVEN_VERTEX );

			#ifdef _TEST_PLAN_
				// Null the details
				levels[current_level_number].geometric_details[i] = 0;
			#endif
		}

		//
		// Simplify
		// Removed odd vertices to go to next coarse level
		//
		itp = levels[current_level_number].pair_contractions.begin();
		while( itp != levels[current_level_number].pair_contractions.end() ) {
		
			// Collapse candidate
			Collapse( *itp );
			
			// Next Pair contrction
			++itp;
		}

		// Update the normals
		ComputeProgressiveFaceNormals();
		ComputeProgressiveVertexNormals();

		// Go to the next coarse level
		current_level_number++;
	}

	// Test plan
	#ifdef _TEST_PLAN_
		Vertex(1844)[1] = -1.0; // plan-2000
//		Vertex(30670)[1] = -1.0; // plan-irregulier
		char filename[255];
		sprintf(filename,"plan-base.wrl");
		WriteFile(filename);
	#endif


	//-- Test --
	//timer.Stop();
	//test<<VertexNumber()<<'\t'<<vnum<<'\t'<<((double)VertexNumber()/vnum - 1.0)*100.0<<'\t'<<timer.Total()<<'\t'<<memory<<endl;
	//--

	//
	// HSI
	//
/*
	#ifdef _HSI_COLOR_
	if( ColorNumber() == VertexNumber() )
	{
		colors = color_backup;
	}
	#endif
*/

	return true;
}
Ejemplo n.º 4
0
static void TranslateVariableName(HLSLCrossCompilerContext* psContext, const Operand* psOperand, uint32_t ui32TOFlag, uint32_t* pui32IgnoreSwizzle)
{
    bstring glsl = *psContext->currentGLSLString;

    *pui32IgnoreSwizzle = 0;

    switch(psOperand->eType)
    {
        case OPERAND_TYPE_IMMEDIATE32:
        {
            if(psOperand->iNumComponents == 1)
            {
				if((ui32TOFlag & TO_FLAG_INTEGER) || psOperand->iIntegerImmediate || fpcheck(psOperand->afImmediates[0]))
				{
					bformata(glsl, "%d",
						*((int*)(&psOperand->afImmediates[0])));
				}
				else
				{
					bformata(glsl, "%f",
                    psOperand->afImmediates[0]);
				}
            }
            else
            if(psOperand->iNumComponents == 4)
            {
                if((ui32TOFlag & TO_FLAG_INTEGER) ||
                    psOperand->iIntegerImmediate ||
                    fpcheck(psOperand->afImmediates[0]) ||
                    fpcheck(psOperand->afImmediates[1]) ||
                    fpcheck(psOperand->afImmediates[2]) ||
                    fpcheck(psOperand->afImmediates[3]))
                {
                    bformata(glsl, "vec4(%d, %d, %d, %d)",
                        *(int*)&psOperand->afImmediates[0],
                        *(int*)&psOperand->afImmediates[1],
                        *(int*)&psOperand->afImmediates[2],
                        *(int*)&psOperand->afImmediates[3]);
                }
                else
                {
                    bformata(glsl, "vec4(%f, %f, %f, %f)",
                        psOperand->afImmediates[0],
                        psOperand->afImmediates[1],
                        psOperand->afImmediates[2],
                        psOperand->afImmediates[3]);
                }
            }
            break;
        }
        case OPERAND_TYPE_INPUT:
        {
            switch(psOperand->iIndexDims)
            {
                case INDEX_2D:
                {
                    if(psOperand->aui32ArraySizes[1] == 0)//Input index zero - position.
                    {
                        bcatcstr(glsl, "gl_in");
                        TranslateOperandIndex(psContext, psOperand, TO_FLAG_NONE);//Vertex index
                        bcatcstr(glsl, ".gl_Position");
                    }
                    else
                    {
                        const char* name = "Input";
                        if(ui32TOFlag & TO_FLAG_DECLARATION_NAME)
                        {
                            name = GetDeclaredName(psContext->psShader->eShaderType, psContext->flags);
                        }
                        
                        bformata(glsl, "%s%d", name, psOperand->aui32ArraySizes[1]);
                        TranslateOperandIndex(psContext, psOperand, TO_FLAG_NONE);//Vertex index
                    }
                    break;
                }
                default:
                {
                    if(psOperand->eIndexRep[0] == OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE)
                    {
                        bformata(glsl, "Input%d[int(", psOperand->ui32RegisterNumber);
                        TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE);
                        bcatcstr(glsl, ")]");
                    }
                    else
                    {
                        if(psContext->psShader->aIndexedInput[psOperand->ui32RegisterNumber] != 0)
                        {
                            const uint32_t parentIndex = psContext->psShader->aIndexedInputParents[psOperand->ui32RegisterNumber];
                            bformata(glsl, "Input%d[%d]", parentIndex,
                                psOperand->ui32RegisterNumber - parentIndex);
                        }
                        else
                        {
                            const char* name = "Input";
                            if(ui32TOFlag & TO_FLAG_DECLARATION_NAME)
                            {
                                name = GetDeclaredName(psContext->psShader->eShaderType, psContext->flags);
                            }

                            bformata(glsl, "%s%d", name, psOperand->ui32RegisterNumber);
                        }
                    }
                    break;
                }
            }
            break;
        }
        case OPERAND_TYPE_OUTPUT:
        {
            bformata(glsl, "Output%d", psOperand->ui32RegisterNumber);
            if(psOperand->psSubOperand[0])
            {
                bcatcstr(glsl, "[int("); //Indexes must be integral.
                TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE);
                bcatcstr(glsl, ")]");
            }
            break;
        }
        case OPERAND_TYPE_OUTPUT_DEPTH:
        case OPERAND_TYPE_OUTPUT_DEPTH_GREATER_EQUAL:
        case OPERAND_TYPE_OUTPUT_DEPTH_LESS_EQUAL:
        {
            bcatcstr(glsl, "gl_FragDepth");
            break;
        }
        case OPERAND_TYPE_TEMP:
        {
            bformata(glsl, "Temp%d", psOperand->ui32RegisterNumber);
            break;
        }
        case OPERAND_TYPE_CONSTANT_BUFFER:
        {
            const char* StageName = "VS";
            ConstantBuffer* psCBuf = NULL;
            GetConstantBufferFromBindingPoint(psOperand->aui32ArraySizes[0], &psContext->psShader->sInfo, &psCBuf);

            switch(psContext->psShader->eShaderType)
            {
                case PIXEL_SHADER:
                {
                    StageName = "PS";
                    break;
                }
                case HULL_SHADER:
                {
                    StageName = "HS";
                    break;
                }
                case DOMAIN_SHADER:
                {
                    StageName = "DS";
                    break;
                }
                case GEOMETRY_SHADER:
                {
                    StageName = "GS";
                    break;
                }
                default:
                {
                    break;
                }
            }


#if CBUFFER_USE_STRUCT_AND_NAMES
            {
                char* pszContBuffName;

                pszContBuffName = psCBuf->Name;
                
                if(psCBuf->Name[0] == '$')//$Global or $Param
                    pszContBuffName++;

                ASSERT(psOperand->aui32ArraySizes[1] < psCBuf->ui32NumVars);
                
                bformata(glsl, "%s%s.%s", pszContBuffName, StageName, psCBuf->asVars[psOperand->aui32ArraySizes[1]].Name);
            }
#else
			if(psContext->flags & HLSLCC_FLAG_UNIFORM_BUFFER_OBJECT)
			{
                if((psCBuf->Name[0] == '$') && (psContext->flags & HLSLCC_FLAG_GLOBAL_CONSTS_NEVER_IN_UBO))
                {
                    bformata(glsl, "Globals%s[%d]", StageName, psOperand->aui32ArraySizes[1]);
                }
                else
                {
				    //Each uniform block is given the HLSL consant buffer name.
				    //Within each uniform block is a constant array named ConstN
				    bformata(glsl, "Const%d[%d]", psOperand->aui32ArraySizes[0], psOperand->aui32ArraySizes[1]);
                }
			}
			else
			{
				//$Globals.
				if(psCBuf->Name[0] == '$')
				{
					bformata(glsl, "Globals%s[%d]", StageName, psOperand->aui32ArraySizes[1]);
				}
				else
				{
					bformata(glsl, "%s%s[%d]", psCBuf->Name, StageName, psOperand->aui32ArraySizes[1]);
				}
			}
#endif
            break;
        }
        case OPERAND_TYPE_RESOURCE:
        {
            TextureName(psContext, psOperand->ui32RegisterNumber, 0);
            break;
        }
        case OPERAND_TYPE_SAMPLER:
        {
            bformata(glsl, "Sampler%d", psOperand->ui32RegisterNumber);
            break;
        }
        case OPERAND_TYPE_FUNCTION_BODY:
        {
            const uint32_t ui32FuncBody = psOperand->ui32RegisterNumber;
            const uint32_t ui32FuncTable = psContext->psShader->aui32FuncBodyToFuncTable[ui32FuncBody];
            //const uint32_t ui32FuncPointer = psContext->psShader->aui32FuncTableToFuncPointer[ui32FuncTable];
            const uint32_t ui32ClassType = psContext->psShader->sInfo.aui32TableIDToTypeID[ui32FuncTable];
            const char* ClassTypeName = &psContext->psShader->sInfo.psClassTypes[ui32ClassType].Name[0];
            const uint32_t ui32UniqueClassFuncIndex = psContext->psShader->ui32NextClassFuncName[ui32ClassType]++;

            bformata(glsl, "%s_Func%d", ClassTypeName, ui32UniqueClassFuncIndex);
            break;
        }
		case OPERAND_TYPE_INPUT_FORK_INSTANCE_ID:
		{
			bcatcstr(glsl, "forkInstanceID");
            *pui32IgnoreSwizzle = 1;
			return;
		}
		case OPERAND_TYPE_IMMEDIATE_CONSTANT_BUFFER:
		{
            bcatcstr(glsl, "immediateConstBufferF");

            if(psOperand->psSubOperand[0])
            {
                bcatcstr(glsl, "(int("); //Indexes must be integral.
                TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE);
                bcatcstr(glsl, "))");
            }
			break;
		}
		case OPERAND_TYPE_INPUT_DOMAIN_POINT:
		{
			bcatcstr(glsl, "gl_TessCoord");
			break;
		}
		case OPERAND_TYPE_INPUT_CONTROL_POINT:
		{
			if(psOperand->aui32ArraySizes[1] == 0)//Input index zero - position.
			{
				bformata(glsl, "gl_in[%d].gl_Position", psOperand->aui32ArraySizes[0]);
			}
            else
            {
                bformata(glsl, "Input%d[%d]", psOperand->aui32ArraySizes[1], psOperand->aui32ArraySizes[0]);
            }
            break;
		}
		case OPERAND_TYPE_NULL:
		{
			// Null register, used to discard results of operations
			bcatcstr(glsl, "//null");
			break;
		}
        case OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID:
        {
            bcatcstr(glsl, "gl_InvocationID");
            *pui32IgnoreSwizzle = 1;
            break;
        }
        case OPERAND_TYPE_OUTPUT_COVERAGE_MASK:
        {
            bcatcstr(glsl, "gl_SampleMask[0]");
            *pui32IgnoreSwizzle = 1;
            break;
        }
        case OPERAND_TYPE_INPUT_COVERAGE_MASK:
        {
            bcatcstr(glsl, "gl_SampleMaskIn[0]");
            //Skip swizzle on scalar types.
            *pui32IgnoreSwizzle = 1;
            break;
        }
        default:
        {
            ASSERT(0);
            break;
        }
    }
}