示例#1
0
 public static void SubFunc(ref CGfxParticlePose r, ref CGfxParticlePose lh, ref CGfxParticlePose rh)
 {
     //r = lh * (1-v) + rh * v;
     r.mPosition = lh.mPosition - rh.mPosition;
     r.mVelocity = lh.mVelocity - rh.mVelocity;
     //r.mAcceleration = lh.mAcceleration - rh.mAcceleration;
     r.mColor    = UInt32_4.Lerp(ref lh.mColor, ref rh.mColor, 0.5f);
     r.mScale    = lh.mScale - rh.mScale;
     r.mRotation = Quaternion.Lerp(lh.mRotation, rh.mRotation, 0.5f);
     r.mAngle    = lh.mAngle - rh.mAngle;
 }
示例#2
0
        public static void Lerp(ref CGfxParticlePose r, ref CGfxParticlePose lh, ref CGfxParticlePose rh, float v)
        {
            //r = lh * (1-v) + rh * v;
            float fa = 1 - v;

            r.mPosition = lh.mPosition * fa + rh.mPosition * v;
            r.mVelocity = lh.mVelocity * fa + rh.mVelocity * v;
            //r.mAcceleration = lh.mAcceleration * fa + rh.mAcceleration * v;
            r.mColor    = UInt32_4.Lerp(ref lh.mColor, ref rh.mColor, v);
            r.mScale    = lh.mScale * fa + rh.mScale * v;
            r.mRotation = Quaternion.Lerp(lh.mRotation, rh.mRotation, v);
            r.mAngle    = lh.mAngle * fa + rh.mAngle * v;
        }
        //实测,在android上,性能分析工具调用本身开心就很大
        //public static Profiler.TimeScope ScopeFind = Profiler.TimeScopeManager.GetTimeScope(typeof(GInstancingManager), "PushInstance.Find", Profiler.TimeScope.EProfileFlag.Windows);
        //public static Profiler.TimeScope ScopePush = Profiler.TimeScopeManager.GetTimeScope(typeof(GInstancingManager), "PushInstance.Push", Profiler.TimeScope.EProfileFlag.Windows);
        public void PushInstance(GInstancingComponent inst, int lightNum, ref UInt32_4 lightIndices, Graphics.Mesh.CGfxMesh oriMesh, EPoolType poolType)
        {
            var meshComp = inst.HostContainer as GMeshComponent;

            if (meshComp == null)
            {
                return;
            }
            //ScopeFind.Begin();
            if (oriMesh.mInstancePool == null || oriMesh.mShadowInstancePool == null)
            {
                oriMesh.mInstancePool = new GInstancing();
                Meshes[oriMesh.Name]  = oriMesh.mInstancePool;

                var task = oriMesh.mInstancePool.Init(oriMesh.Name, false);

                oriMesh.mShadowInstancePool = new GInstancing();
                ShadowMeshes[oriMesh.Name]  = oriMesh.mShadowInstancePool;

                var task1 = oriMesh.mShadowInstancePool.Init(oriMesh.Name, true);
            }
            GInstancing instType = null;

            switch (poolType)
            {
            case EPoolType.Normal:
                instType = oriMesh.mInstancePool;
                break;

            case EPoolType.Shadow:
                instType = oriMesh.mShadowInstancePool;
                break;
            }
            //ScopeFind.End();

            //ScopePush.Begin();
            instType.PushInstance(ref meshComp.Placement.mDrawPosition,
                                  ref meshComp.Placement.mDrawScale,
                                  ref meshComp.Placement.mDrawRotation,
                                  ref lightIndices,
                                  lightNum);
            //ScopePush.End();
        }
        public void PushInstance(ref Vector3 pos, ref Vector3 scale, ref Quaternion quat, ref UInt32_4 f41, int lightNum)
        {
            var rc = CEngine.Instance.RenderContext;

            if (mCurSize == mMaxNumber)
            {
                var savedNum = mMaxNumber;
                mMaxNumber = mCurSize * 2;
                VSInstantData[] newInstData = null;
                Vector3[]       newPos      = null;
                Vector4[]       newScale    = null;
                Quaternion[]    newQuat     = null;
                UInt32_4[]      newF41      = null;

                if (CRenderContext.ShaderModel >= 4)
                {
                    newInstData = new VSInstantData[mMaxNumber];
                }
                else
                {
                    newPos   = new Vector3[mMaxNumber];
                    newScale = new Vector4[mMaxNumber];
                    newQuat  = new Quaternion[mMaxNumber];
                    newF41   = new UInt32_4[mMaxNumber];
                }

                unsafe
                {
                    if (CRenderContext.ShaderModel >= 4)
                    {
                        fixed(VSInstantData *src = &mInstDataArray[0])
                        fixed(VSInstantData * dest = &newInstData[0])
                        {
                            CoreSDK.SDK_Memory_Copy(dest, src, (UInt32)(sizeof(VSInstantData) * savedNum));
                        }

                        var bfDesc = new CGpuBufferDesc();

                        bfDesc.SetMode(true, false);
                        bfDesc.ByteWidth           = (uint)(mMaxNumber * sizeof(VSInstantData));
                        bfDesc.StructureByteStride = (uint)sizeof(VSInstantData);
                        mInstDataBuffer            = rc.CreateGpuBuffer(bfDesc, IntPtr.Zero);

                        var srvDesc = new ISRVDesc();

                        srvDesc.ToDefault();
                        srvDesc.ViewDimension        = EResourceDimension.RESOURCE_DIMENSION_BUFFER;
                        srvDesc.Buffer.ElementOffset = 0;
                        srvDesc.Buffer.NumElements   = (uint)mMaxNumber;
                        mInstDataView = rc.CreateShaderResourceViewFromBuffer(mInstDataBuffer, srvDesc);

                        mAttachSRVs.VSBindTexture(13, mInstDataView);
                    }
                    else
                    {
                        fixed(Vector3 *src = &mPosData[0])
                        fixed(Vector3 * dest = &newPos[0])
                        {
                            CoreSDK.SDK_Memory_Copy(dest, src, (UInt32)(sizeof(Vector3) * savedNum));
                        }

                        fixed(Vector4 *src = &mScaleData[0])
                        fixed(Vector4 * dest = &newScale[0])
                        {
                            CoreSDK.SDK_Memory_Copy(dest, src, (UInt32)(sizeof(Vector3) * savedNum));
                        }

                        fixed(Quaternion *src = &mRotateData[0])
                        fixed(Quaternion * dest = &newQuat[0])
                        {
                            CoreSDK.SDK_Memory_Copy(dest, src, (UInt32)(sizeof(Quaternion) * savedNum));
                        }

                        fixed(UInt32_4 *src = &mF41Data[0])
                        fixed(UInt32_4 * dest = &newF41[0])
                        {
                            CoreSDK.SDK_Memory_Copy(dest, src, (UInt32)(sizeof(UInt32_4) * savedNum));
                        }

                        CVertexBufferDesc desc = new CVertexBufferDesc();

                        desc.ByteWidth = (UInt32)(sizeof(Vector3) * mMaxNumber);
                        desc.Stride    = (UInt32)sizeof(Vector3);
                        mPosVB         = rc.CreateVertexBuffer(desc);

                        desc.ByteWidth = (UInt32)(sizeof(Vector4) * mMaxNumber);
                        desc.Stride    = (UInt32)sizeof(Vector4);
                        mScaleVB       = rc.CreateVertexBuffer(desc);

                        desc.ByteWidth = (UInt32)(sizeof(Quaternion) * mMaxNumber);
                        desc.Stride    = (UInt32)sizeof(Quaternion);
                        mRotateVB      = rc.CreateVertexBuffer(desc);

                        desc.ByteWidth = (UInt32)(sizeof(Vector4) * mMaxNumber);
                        desc.Stride    = (UInt32)sizeof(Vector4);
                        mF41VB         = rc.CreateVertexBuffer(desc);

                        mAttachVBs.BindVertexBuffer(EVertexSteamType.VST_InstPos, mPosVB);
                        mAttachVBs.BindVertexBuffer(EVertexSteamType.VST_InstScale, mScaleVB);
                        mAttachVBs.BindVertexBuffer(EVertexSteamType.VST_InstQuat, mRotateVB);
                        mAttachVBs.BindVertexBuffer(EVertexSteamType.VST_F4_1, mF41VB);
                    }
                }

                mInstDataArray = newInstData;
                mPosData       = newPos;
                mScaleData     = newScale;
                mRotateData    = newQuat;
                mF41Data       = newF41;
            }

            if (CRenderContext.ShaderModel >= 4)
            {
                mInstDataArray[mCurSize].WorldMatrix = Matrix.Transformation(scale, quat, pos);
                mInstDataArray[mCurSize].WorldMatrix.Transpose();
                mInstDataArray[mCurSize].CustomData.x      = (uint)lightNum;
                mInstDataArray[mCurSize].PointLightIndices = f41;
            }
            else
            {
                mPosData[mCurSize]     = pos;
                mScaleData[mCurSize].X = scale.X;
                mScaleData[mCurSize].Y = scale.Y;
                mScaleData[mCurSize].Z = scale.Z;
                mScaleData[mCurSize].W = lightNum;
                mRotateData[mCurSize]  = quat;
                mF41Data[mCurSize]     = f41;
            }
            mCurSize++;
        }
        public async System.Threading.Tasks.Task Init(RName name, bool shadow)
        {
            mMaxNumber     = DefaultMaxNumber;
            mInstDataArray = new VSInstantData[mMaxNumber];
            mPosData       = new Vector3[mMaxNumber];
            mScaleData     = new Vector4[mMaxNumber];
            mRotateData    = new Quaternion[mMaxNumber];
            mF41Data       = new UInt32_4[mMaxNumber];

            unsafe
            {
                var rc = CEngine.Instance.RenderContext;
                CVertexBufferDesc desc = new CVertexBufferDesc();
                desc.CPUAccess = (UInt32)ECpuAccess.CAS_WRITE;
                desc.ByteWidth = (UInt32)(sizeof(Vector3) * mMaxNumber);
                desc.Stride    = (UInt32)sizeof(Vector3);
                mPosVB         = rc.CreateVertexBuffer(desc);

                desc.ByteWidth = (UInt32)(sizeof(Vector4) * mMaxNumber);
                desc.Stride    = (UInt32)sizeof(Vector4);
                mScaleVB       = rc.CreateVertexBuffer(desc);

                desc.ByteWidth = (UInt32)(sizeof(Quaternion) * mMaxNumber);
                desc.Stride    = (UInt32)sizeof(Quaternion);
                mRotateVB      = rc.CreateVertexBuffer(desc);

                desc.ByteWidth = (UInt32)(sizeof(Vector4) * mMaxNumber);
                desc.Stride    = (UInt32)sizeof(Vector4);
                mF41VB         = rc.CreateVertexBuffer(desc);

                var bfDesc = new CGpuBufferDesc();
                bfDesc.SetMode(true, false);
                bfDesc.ByteWidth           = (uint)(mMaxNumber * sizeof(VSInstantData));
                bfDesc.StructureByteStride = (uint)sizeof(VSInstantData);
                mInstDataBuffer            = rc.CreateGpuBuffer(bfDesc, IntPtr.Zero);

                var srvDesc = new ISRVDesc();
                srvDesc.ToDefault();
                srvDesc.ViewDimension        = EResourceDimension.RESOURCE_DIMENSION_BUFFER;
                srvDesc.Buffer.ElementOffset = 0;
                srvDesc.Buffer.NumElements   = (uint)mMaxNumber;
                mInstDataView = rc.CreateShaderResourceViewFromBuffer(mInstDataBuffer, srvDesc);
            }

            MeshActor = await Actor.GActor.NewMeshActorAsync(name);

            MeshComp = MeshActor.GetComponent <GMeshComponent>();
            var modifier = MeshComp.SceneMesh.MdfQueue.FindModifier <CGfxInstancingModifier>();

            if (modifier == null)
            {
                modifier = new CGfxInstancingModifier();
                MeshComp.SceneMesh.MdfQueue.AddModifier(modifier);
            }
            if (shadow)
            {
                modifier.SetShadowHost(this);
            }
            else
            {
                modifier.SetHost(this);
            }

            MeshComp.IsIdentityDrawTransform = true;
            MeshActor.Placement.Location     = Vector3.Zero;
            CEngine.Instance.HitProxyManager.MapActor(MeshActor);

            mAttachVBs.BindVertexBuffer(EVertexSteamType.VST_InstPos, mPosVB);
            mAttachVBs.BindVertexBuffer(EVertexSteamType.VST_InstScale, mScaleVB);
            mAttachVBs.BindVertexBuffer(EVertexSteamType.VST_InstQuat, mRotateVB);
            mAttachVBs.BindVertexBuffer(EVertexSteamType.VST_F4_1, mF41VB);

            mAttachSRVs.VSBindTexture(13, mInstDataView);
        }