Пример #1
0
        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++;
        }
Пример #2
0
        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);
        }