/// <summary> /// Constructs a new instance of the <see cref="MipData"/> class. /// </summary> /// <param name="bitmap">FreeImage bitmap to wrap or copy from.</param> /// <param name="copyData">True to copy the image data, false to wrap the free image bitmap data.</param> /// <exception cref="ArgumentException">Thrown if the bitmap is not valid.</exception> public MipData(Surface bitmap, bool copyData = false) { if (bitmap == null || bitmap.IsDisposed) { throw new ArgumentException("Surface is not valid."); } m_isDisposed = false; Width = bitmap.Width; Height = bitmap.Height; Depth = 1; RowPitch = bitmap.Pitch; SlicePitch = RowPitch * Height; if (!copyData) { m_ownData = false; Data = bitmap.DataPtr; } else { m_ownData = true; int sizeInBytes = SizeInBytes; Data = MemoryHelper.AllocateMemory(sizeInBytes); MemoryHelper.CopyMemory(Data, bitmap.DataPtr, sizeInBytes); AddGCMemoryPressure(); } }
/// <summary> /// Writes the managed data to the native value. /// </summary> /// <param name="thisPtr">Optional pointer to the memory that will hold the native value.</param> /// <param name="nativeValue">Output native value</param> void IMarshalable <Node, AiNode> .ToNative(IntPtr thisPtr, out AiNode nativeValue) { nativeValue = new AiNode(); // -------------------------------- nativeValue.Name = new AiString(m_name); nativeValue.Transformation = AssimpConv.ConvertTransformToAssimp(m_transform); nativeValue.Parent = IntPtr.Zero; nativeValue.NumMeshes = (uint)m_meshes.Count; nativeValue.Meshes = IntPtr.Zero; #if WITH_NODE_METADATA nativeValue.MetaData = IntPtr.Zero; #endif if (nativeValue.NumMeshes > 0) { nativeValue.Meshes = MemoryHelper.ToNativeArray <int>(m_meshes.ToArray()); } //Now descend through the children nativeValue.NumChildren = (uint)m_children.Count; int numChildren = (int)nativeValue.NumChildren; int stride = IntPtr.Size; IntPtr childrenPtr = IntPtr.Zero; if (numChildren > 0) { childrenPtr = MemoryHelper.AllocateMemory(numChildren * IntPtr.Size); for (int i = 0; i < numChildren; i++) { IntPtr currPos = MemoryHelper.AddIntPtr(childrenPtr, stride * i); Node child = m_children[i]; IntPtr childPtr = IntPtr.Zero; //Recursively create the children and its children if (child != null) { childPtr = ToNativeRecursive(thisPtr, child); } //Write the child's node ptr to our array MemoryHelper.Write <IntPtr>(currPos, ref childPtr); } } //Finally finish writing to the native struct nativeValue.Children = childrenPtr; }
/// <summary> /// Constructs a new instance of the <see cref="MipData"/> class. Represents a 3D image. This allocates the owned memory. /// </summary> /// <param name="width">Width of the image, in texels.</param> /// <param name="height">Height of the image, in texels.</param> /// <param name="depth">Depth of the image, in texels.</param> /// <param name="rowPitch">Row pitch.</param> /// <param name="slicePitch">Slice pitch.</param> public MipData(int width, int height, int depth, int rowPitch, int slicePitch) { m_isDisposed = false; Width = width; Height = height; Depth = depth; RowPitch = rowPitch; SlicePitch = slicePitch; m_ownData = true; Data = MemoryHelper.AllocateMemory(SizeInBytes); AddGCMemoryPressure(); }
private IntPtr ToNativeRecursive(IntPtr parentPtr, Node node) { if (node == null) { return(IntPtr.Zero); } int sizeofNative = MemoryHelper.SizeOf <AiNode>(); //Allocate the memory that will hold the node IntPtr nodePtr = MemoryHelper.AllocateMemory(sizeofNative); //First fill the native struct AiNode nativeValue = new AiNode(); nativeValue.Name = new AiString(node.m_name); nativeValue.Transformation = AssimpConv.ConvertTransformToAssimp(node.m_transform); nativeValue.Parent = parentPtr; nativeValue.NumMeshes = (uint)node.m_meshes.Count; nativeValue.Meshes = MemoryHelper.ToNativeArray <int>(node.m_meshes.ToArray()); #if WITH_NODE_METADATA nativeValue.MetaData = IntPtr.Zero; #endif //Now descend through the children nativeValue.NumChildren = (uint)node.m_children.Count; int numChildren = (int)nativeValue.NumChildren; int stride = IntPtr.Size; IntPtr childrenPtr = IntPtr.Zero; if (numChildren > 0) { childrenPtr = MemoryHelper.AllocateMemory(numChildren * IntPtr.Size); for (int i = 0; i < numChildren; i++) { IntPtr currPos = MemoryHelper.AddIntPtr(childrenPtr, stride * i); Node child = node.m_children[i]; IntPtr childPtr = IntPtr.Zero; //Recursively create the children and its children if (child != null) { childPtr = ToNativeRecursive(nodePtr, child); } //Write the child's node ptr to our array MemoryHelper.Write <IntPtr>(currPos, ref childPtr); } } //Finall finish writing to the native struct, and write the whole thing to the memory we allocated previously nativeValue.Children = childrenPtr; MemoryHelper.Write <AiNode>(nodePtr, ref nativeValue); return(nodePtr); }