Example #1
0
        protected override bool AddFunctionInvocations(ProgramSet programSet)
        {
            Program            vsProgram         = programSet.CpuVertexProgram;
            Program            psProgram         = programSet.CpuFragmentProgram;
            Function           psMain            = psProgram.EntryPointFunction;
            Function           vsMain            = vsProgram.EntryPointFunction;
            FunctionInvocation curFuncInvocation = null;
            //Calculate the position and size of the texture in the atlas in the vertex shader
            int groupOrder = ((int)FFPRenderState.FFPVertexShaderStage.VSTexturing -
                              (int)FFPRenderState.FFPVertexShaderStage.VSLighting) / 2;
            int internalCounter = 0;

            for (int i = 0; i < TextureAtlasSampler.MaxTextures; i++)
            {
                if (this.isAtlasTextureUnits[i] == true)
                {
                    Operand.OpMask textureIndexMask = Operand.OpMask.X;
                    switch (i)
                    {
                    case 1:
                        textureIndexMask = Operand.OpMask.Y;
                        break;

                    case 2:
                        textureIndexMask = Operand.OpMask.Z;
                        break;

                    case 3:
                        textureIndexMask = Operand.OpMask.W;
                        break;
                    }
                    curFuncInvocation = new FunctionInvocation(FFPRenderState.FFPFuncAssign, groupOrder,
                                                               internalCounter++);
                    curFuncInvocation.PushOperand(this.vsTextureTable[i], Operand.OpSemantic.In);
                    curFuncInvocation.PushOperand(this.vsInpTextureTableIndex, Operand.OpSemantic.In, (int)textureIndexMask,
                                                  1);
                    curFuncInvocation.PushOperand(this.vsOutTextureDatas[i], Operand.OpSemantic.Out);
                    vsMain.AddAtomInstance(curFuncInvocation);
                }
            }

            //sample the texture in the fragment shader given the extracted data in the pixel shader
            // groupOrder = (FFP_PS_SAMPLING + FFP_PS_TEXTURING) / 2;
            internalCounter = 0;

            var inpParams   = psMain.InputParameters;
            var localParams = psMain.LocalParameters;

            Parameter psAtlasTextureCoord = psMain.ResolveLocalParameter(Parameter.SemanticType.Unknown, -1,
                                                                         "atlasCoord",
                                                                         GpuProgramParameters.GpuConstantType.Float2);

            for (int j = 0; j < TextureAtlasSampler.MaxTextures; j++)
            {
                if (this.isAtlasTextureUnits[j] == true)
                {
                    //Find the texture coordinates texel and sampler from the original FFPTexturing
                    Parameter texcoord = Function.GetParameterByContent(inpParams,
                                                                        (Parameter.ContentType)
                                                                            ((int)Parameter.ContentType.TextureCoordinate0 +
                                                                            j),
                                                                        GpuProgramParameters.GpuConstantType.Float2);
                    Parameter        texel   = Function.GetParameterByName(localParams, this.paramTexel + j.ToString());
                    UniformParameter sampler =
                        psProgram.GetParameterByType(GpuProgramParameters.GpuConstantType.Sampler2D, j);

                    //TODO
                    string addressUFuncName = GetAddressingFunctionName(this.textureAddressings[j].U);
                    string addressVFuncName = GetAddressingFunctionName(this.textureAddressings[j].V);

                    //Create a function which will replace the texel with the texture texel
                    if ((texcoord != null) && (texel != null) && (sampler != null) &&
                        (addressUFuncName != null) && (addressVFuncName != null))
                    {
                        //calculate the U value due to addressing mode
                        curFuncInvocation = new FunctionInvocation(addressUFuncName, groupOrder, internalCounter++);
                        curFuncInvocation.PushOperand(texcoord, Operand.OpSemantic.In, Operand.OpMask.X);
                        curFuncInvocation.PushOperand(psAtlasTextureCoord, Operand.OpSemantic.Out, Operand.OpMask.X);
                        psMain.AddAtomInstance(curFuncInvocation);

                        //calculate the V value due to addressing mode
                        curFuncInvocation = new FunctionInvocation(addressVFuncName, groupOrder, internalCounter++);
                        curFuncInvocation.PushOperand(texcoord, Operand.OpSemantic.In, Operand.OpMask.Y);
                        curFuncInvocation.PushOperand(psAtlasTextureCoord, Operand.OpSemantic.Out, Operand.OpMask.Y);
                        psMain.AddAtomInstance(curFuncInvocation);

                        //sample the texel color
                        curFuncInvocation =
                            new FunctionInvocation(this.autoAdjustPollPosition ? SGXFuncAtlasSampleAutoAdjust : SGXFuncAtlasSampleNormal,
                                                   groupOrder, internalCounter++);
                        curFuncInvocation.PushOperand(sampler, Operand.OpSemantic.In);
                        curFuncInvocation.PushOperand(texcoord, Operand.OpSemantic.In, (int)(Operand.OpMask.X | Operand.OpMask.Y));
                        curFuncInvocation.PushOperand(psAtlasTextureCoord, Operand.OpSemantic.In);
                        curFuncInvocation.PushOperand(this.psInpTextureData[j], Operand.OpSemantic.In);
                        curFuncInvocation.PushOperand(this.psTextureSizes[j], Operand.OpSemantic.In);
                        curFuncInvocation.PushOperand(texel, Operand.OpSemantic.Out);
                        psMain.AddAtomInstance(curFuncInvocation);
                    }
                }
            }

            return(true);
        }