protected unsafe void SetProxyConstants(ref MyRenderableProxy_2 proxy, MyMergeInstancingConstants?arg = null) { MyMergeInstancingConstants constants = arg ?? MyMergeInstancingConstants.Default; int version = constants.GetHashCode(); if (constants.GetHashCode() != proxy.ObjectConstants.Version) { int size = sizeof(MyMergeInstancingConstants); var buffer = new byte[sizeof(MyMergeInstancingConstants)]; proxy.ObjectConstants = new MyConstantsPack() { BindFlag = MyBindFlag.BIND_VS, CB = MyCommon.GetObjectCB(size), Version = version, Data = buffer }; fixed(byte *dstPtr = buffer) { #if XB1 SharpDX.Utilities.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constants), size); #else // !XB1 MyMemory.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constants), (uint)size); #endif // !XB1 } } MyRenderUtils.MoveConstants(RC, ref proxy.ObjectConstants); MyRenderUtils.SetConstants(RC, ref proxy.ObjectConstants, MyCommon.OBJECT_SLOT); ++Stats.ObjectConstantsChanges; }
protected override unsafe void OnFrame(byte[] frameData) { var mapping = MyMapping.MapDiscard(m_texture.Resource); var lineSize = (uint)(SharpDX.DXGI.FormatHelper.SizeOfInBytes(VideoFormat) * VideoWidth); var rowPitch = mapping.dataBox.RowPitch; fixed(byte *ptr = frameData) { for (int y = 0; y < VideoHeight; y++) { var dst = new IntPtr((byte *)mapping.dataBox.DataPointer.ToPointer() + rowPitch * y); var src = new IntPtr((byte *)ptr + lineSize * y); MyMemory.CopyMemory(dst, src, lineSize); } } mapping.Unmap(); }
internal unsafe void MoveConstants(ref MyConstantsPack desc) { if (desc.CB == null) { return; } if (State.m_constantsVersion.Get(desc.CB) != desc.Version) { State.m_constantsVersion[desc.CB] = desc.Version; var box = Context.MapSubresource((SharpDX.Direct3D11.Resource)desc.CB, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None); // TODO: try with aligned memory fixed(byte *ptr = desc.Data) { MyMemory.CopyMemory(box.DataPointer, new IntPtr(ptr), (uint)desc.Data.Length); } Context.UnmapSubresource(desc.CB, 0); } }
static unsafe MyMaterialProxy_2 CreateProxy(MyVoxelMaterialTriple triple) { byte[] buffer; int size; System.Diagnostics.Debug.Assert(triple.I0 < Table.Length, "Index to table incorrect"); System.Diagnostics.Debug.Assert(triple.I1 < Table.Length, "Index to table incorrect"); System.Diagnostics.Debug.Assert(triple.I2 < Table.Length, "Index to table incorrect"); //TODO: This shouldnt happen if Table is created correctly if (triple.I0 >= Table.Length) { triple.I0 = 0; } if (triple.I1 >= Table.Length) { triple.I1 = -1; } if (triple.I2 >= Table.Length) { triple.I2 = -1; } //////end of hack bool singleMaterial = triple.I1 == -1 && triple.I2 == -1; if (singleMaterial) { size = sizeof(MyVoxelMaterialConstants); MyVoxelMaterialConstants constantsData = new MyVoxelMaterialConstants(); constantsData.DistancesAndScale = Table[triple.I0].DistanceAndScale; constantsData.DistancesAndScaleFar = Table[triple.I0].DistanceAndScaleFar; constantsData.DistancesAndScaleFar3 = Table[triple.I0].DistanceAndScaleFar3; constantsData.Far3Color = Table[triple.I0].Far3Color; constantsData.ExtensionDetailScale = Table[triple.I0].ExtensionDetailScale; buffer = new byte[size]; fixed(byte *dstPtr = buffer) { MyMemory.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constantsData), (uint)size); } } else { size = sizeof(MyVoxelMultiMaterialConstants); MyVoxelMultiMaterialConstants constantsData = new MyVoxelMultiMaterialConstants(); constantsData.DistancesAndScale0 = Table[triple.I0].DistanceAndScale; constantsData.DistancesAndScale1 = Table[triple.I1].DistanceAndScale; constantsData.DistancesAndScale2 = triple.I2 >= 0 ? Table[triple.I2].DistanceAndScale : Vector4.Zero; constantsData.DistancesAndScaleFar0 = Table[triple.I0].DistanceAndScaleFar; constantsData.DistancesAndScaleFar1 = Table[triple.I1].DistanceAndScaleFar; constantsData.DistancesAndScaleFar2 = triple.I2 >= 0 ? Table[triple.I2].DistanceAndScaleFar : Vector4.Zero; constantsData.DistancesAndScaleFar31 = new Vector4(Table[triple.I0].DistanceAndScaleFar3.X, Table[triple.I0].DistanceAndScaleFar3.Y, 0, 0); constantsData.DistancesAndScaleFar32 = new Vector4(Table[triple.I1].DistanceAndScaleFar3.X, Table[triple.I1].DistanceAndScaleFar3.Y, 0, 0); constantsData.DistancesAndScaleFar33 = triple.I2 >= 0 ? new Vector4(Table[triple.I2].DistanceAndScaleFar3.X, Table[triple.I2].DistanceAndScaleFar3.Y, 0, 0) : Vector4.Zero; constantsData.Far3Color1 = Table[triple.I0].Far3Color.ToVector4(); constantsData.Far3Color2 = Table[triple.I1].Far3Color.ToVector4(); constantsData.Far3Color3 = triple.I2 >= 0 ? Table[triple.I2].Far3Color.ToVector4() : Vector4.Zero; constantsData.ExtensionDetailScale0 = Table[triple.I0].ExtensionDetailScale; constantsData.ExtensionDetailScale1 = Table[triple.I1].ExtensionDetailScale; constantsData.ExtensionDetailScale2 = triple.I2 >= 0 ? Table[triple.I2].ExtensionDetailScale : 0; buffer = new byte[size]; fixed(byte *dstPtr = buffer) { MyMemory.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constantsData), (uint)size); } } var version = triple.I0.GetHashCode(); MyHashHelper.Combine(ref version, triple.I1.GetHashCode()); MyHashHelper.Combine(ref version, triple.I2.GetHashCode()); return(new MyMaterialProxy_2 { MaterialConstants = { BindFlag = MyBindFlag.BIND_PS, CB = MyCommon.GetMaterialCB(size), Version = version, Data = buffer }, MaterialSRVs = { // NOTE(AF) Adding BIND_VS here will interfere with shadows, causing flickering in the first cascade BindFlag = MyBindFlag.BIND_PS, StartSlot = 0, Version = version, SRVs = singleMaterial ? new ShaderResourceView[] { Table[triple.I0].Near.ColorMetalArray.ShaderView, Table[triple.I0].Far1.ColorMetalArray.ShaderView, Table[triple.I0].Far2.ColorMetalArray.ShaderView, Table[triple.I0].Near.NormalGlossArray.ShaderView, Table[triple.I0].Far1.NormalGlossArray.ShaderView, Table[triple.I0].Far2.NormalGlossArray.ShaderView, Table[triple.I0].Near.ExtArray.ShaderView, Table[triple.I0].Far1.ExtArray.ShaderView, Table[triple.I0].Far2.ExtArray.ShaderView, } : ( triple.I2 == -1 ? new ShaderResourceView[] { Table[triple.I0].Near.ColorMetalArray.ShaderView, Table[triple.I0].Far1.ColorMetalArray.ShaderView, Table[triple.I0].Far2.ColorMetalArray.ShaderView, Table[triple.I1].Near.ColorMetalArray.ShaderView, Table[triple.I1].Far1.ColorMetalArray.ShaderView, Table[triple.I1].Far2.ColorMetalArray.ShaderView, null, null, null, Table[triple.I0].Near.NormalGlossArray.ShaderView, Table[triple.I0].Far1.NormalGlossArray.ShaderView, Table[triple.I0].Far2.NormalGlossArray.ShaderView, Table[triple.I1].Near.NormalGlossArray.ShaderView, Table[triple.I1].Far1.NormalGlossArray.ShaderView, Table[triple.I1].Far2.NormalGlossArray.ShaderView, null, null, null, Table[triple.I0].Near.ExtArray.ShaderView, Table[triple.I0].Far1.ExtArray.ShaderView, Table[triple.I0].Far2.ExtArray.ShaderView, Table[triple.I1].Near.ExtArray.ShaderView, Table[triple.I1].Far1.ExtArray.ShaderView, Table[triple.I1].Far2.ExtArray.ShaderView, null, null, null } : new ShaderResourceView[] { Table[triple.I0].Near.ColorMetalArray.ShaderView, Table[triple.I0].Far1.ColorMetalArray.ShaderView, Table[triple.I0].Far2.ColorMetalArray.ShaderView, Table[triple.I1].Near.ColorMetalArray.ShaderView, Table[triple.I1].Far1.ColorMetalArray.ShaderView, Table[triple.I1].Far2.ColorMetalArray.ShaderView, Table[triple.I2].Near.ColorMetalArray.ShaderView, Table[triple.I2].Far1.ColorMetalArray.ShaderView, Table[triple.I2].Far2.ColorMetalArray.ShaderView, Table[triple.I0].Near.NormalGlossArray.ShaderView, Table[triple.I0].Far1.NormalGlossArray.ShaderView, Table[triple.I0].Far2.NormalGlossArray.ShaderView, Table[triple.I1].Near.NormalGlossArray.ShaderView, Table[triple.I1].Far1.NormalGlossArray.ShaderView, Table[triple.I1].Far2.NormalGlossArray.ShaderView, Table[triple.I2].Near.NormalGlossArray.ShaderView, Table[triple.I2].Far1.NormalGlossArray.ShaderView, Table[triple.I2].Far2.NormalGlossArray.ShaderView, Table[triple.I0].Near.ExtArray.ShaderView, Table[triple.I0].Far1.ExtArray.ShaderView, Table[triple.I0].Far2.ExtArray.ShaderView, Table[triple.I1].Near.ExtArray.ShaderView, Table[triple.I1].Far1.ExtArray.ShaderView, Table[triple.I1].Far2.ExtArray.ShaderView, Table[triple.I2].Near.ExtArray.ShaderView, Table[triple.I2].Far1.ExtArray.ShaderView, Table[triple.I2].Far2.ExtArray.ShaderView, } ) } }); }
static unsafe MyMaterialProxy_2 CreateProxy(MyVoxelMaterialTriple triple) { byte[] buffer; int size; bool singleMaterial = triple.I1 == -1 && triple.I2 == -1; if (singleMaterial) { size = sizeof(MyVoxelMaterialConstants); MyVoxelMaterialConstants constantsData = new MyVoxelMaterialConstants(); constantsData.Scales = Table[triple.I0].ScaleFactors; buffer = new byte[size]; fixed(byte *dstPtr = buffer) { MyMemory.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constantsData), (uint)size); } } else { size = sizeof(MyVoxelMultiMaterialConstants); MyVoxelMultiMaterialConstants constantsData = new MyVoxelMultiMaterialConstants(); constantsData.Scales0 = Table[triple.I0].ScaleFactors; constantsData.Scales1 = Table[triple.I1].ScaleFactors; constantsData.Scales2 = triple.I2 >= 0 ? Table[triple.I2].ScaleFactors : Vector3.One; buffer = new byte[size]; fixed(byte *dstPtr = buffer) { MyMemory.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constantsData), (uint)size); } } var version = triple.I0.GetHashCode(); MyHashHelper.Combine(ref version, triple.I1.GetHashCode()); MyHashHelper.Combine(ref version, triple.I2.GetHashCode()); return(new MyMaterialProxy_2 { MaterialConstants = { BindFlag = MyBindFlag.BIND_PS, CB = MyCommon.GetMaterialCB(size), Version = version, Data = buffer }, MaterialSRVs = { BindFlag = MyBindFlag.BIND_PS, StartSlot = 0, Version = version, SRVs = singleMaterial ? new ShaderResourceView[] { Table[triple.I0].Near.ColorMetalArray.ShaderView, Table[triple.I0].Far1.ColorMetalArray.ShaderView, Table[triple.I0].Far2.ColorMetalArray.ShaderView, Table[triple.I0].Near.NormalGlossArray.ShaderView, Table[triple.I0].Far1.NormalGlossArray.ShaderView, Table[triple.I0].Far2.NormalGlossArray.ShaderView, } : ( triple.I2 == -1 ? new ShaderResourceView[] { Table[triple.I0].Near.ColorMetalArray.ShaderView, Table[triple.I0].Far1.ColorMetalArray.ShaderView, Table[triple.I0].Far2.ColorMetalArray.ShaderView, Table[triple.I1].Near.ColorMetalArray.ShaderView, Table[triple.I1].Far1.ColorMetalArray.ShaderView, Table[triple.I1].Far2.ColorMetalArray.ShaderView, null, null, null, Table[triple.I0].Near.NormalGlossArray.ShaderView, Table[triple.I0].Far1.NormalGlossArray.ShaderView, Table[triple.I0].Far2.NormalGlossArray.ShaderView, Table[triple.I1].Near.NormalGlossArray.ShaderView, Table[triple.I1].Far1.NormalGlossArray.ShaderView, Table[triple.I1].Far2.NormalGlossArray.ShaderView, null, null, null, null, null, null, null, null, null, null, null, null } : new ShaderResourceView[] { Table[triple.I0].Near.ColorMetalArray.ShaderView, Table[triple.I0].Far1.ColorMetalArray.ShaderView, Table[triple.I0].Far2.ColorMetalArray.ShaderView, Table[triple.I1].Near.ColorMetalArray.ShaderView, Table[triple.I1].Far1.ColorMetalArray.ShaderView, Table[triple.I1].Far2.ColorMetalArray.ShaderView, Table[triple.I2].Near.ColorMetalArray.ShaderView, Table[triple.I2].Far1.ColorMetalArray.ShaderView, Table[triple.I2].Far2.ColorMetalArray.ShaderView, Table[triple.I0].Near.NormalGlossArray.ShaderView, Table[triple.I0].Far1.NormalGlossArray.ShaderView, Table[triple.I0].Far2.NormalGlossArray.ShaderView, Table[triple.I1].Near.NormalGlossArray.ShaderView, Table[triple.I1].Far1.NormalGlossArray.ShaderView, Table[triple.I1].Far2.NormalGlossArray.ShaderView, Table[triple.I2].Near.NormalGlossArray.ShaderView, Table[triple.I2].Far1.NormalGlossArray.ShaderView, Table[triple.I2].Far2.NormalGlossArray.ShaderView, null, null, null, null, null, null, null, null, null } ) } }); }
static unsafe MyMaterialProxy_2 CreateProxy(MyVoxelMaterialTriple triple) { byte[] buffer; int size; MyRenderProxy.Assert(triple.I0 < Table.Length, "Index to table incorrect"); MyRenderProxy.Assert(triple.I1 < Table.Length, "Index to table incorrect"); MyRenderProxy.Assert(triple.I2 < Table.Length, "Index to table incorrect"); //TODO: This shouldnt happen if Table is created correctly if (triple.I0 >= Table.Length) { triple.I0 = 0; } if (triple.I1 >= Table.Length) { triple.I1 = -1; } if (triple.I2 >= Table.Length) { triple.I2 = -1; } //////end of hack bool singleMaterial = triple.I1 == -1 && triple.I2 == -1; if (singleMaterial) { size = sizeof(MyVoxelMaterialConstants); MyVoxelMaterialConstants constantsData = new MyVoxelMaterialConstants(); FillVoxelMaterialEntry(ref constantsData.entry, ref Table[triple.I0]); buffer = new byte[size]; fixed(byte *dstPtr = buffer) { #if XB1 SharpDX.Utilities.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constantsData), size); #else // !XB1 MyMemory.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constantsData), (uint)size); #endif // !XB1 } } else { size = sizeof(MyVoxelMultiMaterialConstants); MyVoxelMultiMaterialConstants constantsData = new MyVoxelMultiMaterialConstants(); FillVoxelMaterialEntry(ref constantsData.entry0, ref Table[triple.I0]); FillVoxelMaterialEntry(ref constantsData.entry1, ref Table[triple.I1]); if (triple.I2 >= 0) { FillVoxelMaterialEntry(ref constantsData.entry2, ref Table[triple.I2]); } else { ResetVoxelMaterialEntry(out constantsData.entry2); } buffer = new byte[size]; fixed(byte *dstPtr = buffer) { #if XB1 SharpDX.Utilities.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constantsData), size); #else // !XB1 MyMemory.CopyMemory(new IntPtr(dstPtr), new IntPtr(&constantsData), (uint)size); #endif // !XB1 } } var version = triple.I0.GetHashCode(); MyHashHelper.Combine(ref version, triple.I1.GetHashCode()); MyHashHelper.Combine(ref version, triple.I2.GetHashCode()); MyConstantsPack materialConstants = new MyConstantsPack { BindFlag = MyBindFlag.BIND_PS, CB = MyCommon.GetMaterialCB(size), Version = version, Data = buffer }; MySrvTable srvTable = new MySrvTable { // NOTE(AF) Adding BIND_VS here will interfere with shadows, causing flickering in the first cascade BindFlag = MyBindFlag.BIND_PS, StartSlot = 0, Version = version, Srvs = new ISrvBindable[] { MyGlobalResources.FileArrayTextureVoxelCM, MyGlobalResources.FileArrayTextureVoxelNG, MyGlobalResources.FileArrayTextureVoxelExt, } }; return(new MyMaterialProxy_2 { MaterialConstants = materialConstants, MaterialSrvs = srvTable }); }