public void CopyPixelDataToLayer <T>(NativeArray <T> colorData, int layerIdx, GraphicsFormat format) where T : struct { var layer = GetLayer(layerIdx); NativeArray <T> dstDataAsColor; AtomicSafetyHandle safety = AtomicSafetyHandle.Create(); unsafe { dstDataAsColor = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>(layer.data, layer.dataSize, Allocator.None); NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref dstDataAsColor, safety); } var dstWidth = layer.scanlineSize / UnsafeUtility.SizeOf <T>(); int scanLines = height / (int)GraphicsFormatUtility.GetBlockHeight(format); int pitch = (width * (int)GraphicsFormatUtility.GetBlockSize(format)) / ((int)GraphicsFormatUtility.GetBlockWidth(format) * UnsafeUtility.SizeOf <T>()); if (scanLines * pitch > colorData.Length) { throw new ArgumentException($"Could not copy from ColorData in layer {layer}, {format}. The Provided source array is smaller than the tile content."); } if ((scanLines - 1) * dstWidth + pitch > dstDataAsColor.Length) { throw new ArgumentException($"Trying to write outside of the layer {layer} data buffer bounds. Is the provided format {format} correct?"); } for (int i = 0; i < scanLines; ++i) { NativeArray <T> .Copy(colorData, i *pitch, dstDataAsColor, i *dstWidth, pitch); } dstDataAsColor.Dispose(); AtomicSafetyHandle.Release(safety); }
public static Rect[] PackTexture(out Texture2D atlas_, Texture2D[] texture_, int size_, int padding_) { //TODO test on device var format = texture_[0].graphicsFormat; if (!SystemInfo.IsFormatSupported(format, FormatUsage.Sample)) { Log.Warning($"unsupported format {format}"); atlas_ = new Texture2D(1, 1); return(atlas_.PackTextures(texture_, padding_, size_)); } var rect = new System.Collections.Generic.List <Rect>(texture_.Length); var texArray = texture_.Select(t => new Vector2(t.width, t.height)).ToArray(); if (!Texture2D.GenerateAtlas(texArray, padding_, size_, rect)) { Log.Error("failed to pack textures"); atlas_ = null; return(null); } atlas_ = new Texture2D(size_, size_, format, TextureCreationFlags.None); //etc2+eac 8bpp 4x4 = 128bits //dxt5/bc3 8bpp 4x4 = 128bits //pvrtc 4bpp: 4x4 = 64bits 2bpp: 8x4=64bits //astc fixed 128bits var blockSize = GraphicsFormatUtility.GetBlockSize(format); var blockWidth = (int)GraphicsFormatUtility.GetBlockWidth(format); var blockHeight = (int)GraphicsFormatUtility.GetBlockHeight(format); if (blockSize == 16) { CopyTextureRawData <Block128Bit>(atlas_, texture_, rect, blockWidth, blockHeight); } else if (blockSize == 8) { CopyTextureRawData <UInt64>(atlas_, texture_, rect, blockWidth, blockHeight); } else if (blockSize == 4) { CopyTextureRawData <UInt32>(atlas_, texture_, rect, blockWidth, blockHeight); } else { Log.Warning($"unsupported block size {blockSize} byte"); } atlas_.Apply(false, true); float sizeInv = 1f / size_; for (int k = 0; k < rect.Count; ++k) { var r = rect[k]; rect[k] = new Rect(r.x * sizeInv, r.y * sizeInv, r.width * sizeInv, r.height * sizeInv); } return(rect.ToArray()); }
internal bool ValidateSize(int width, int height, GraphicsFormat format) { if (GraphicsFormatUtility.GetBlockSize(format) * (width / GraphicsFormatUtility.GetBlockWidth(format)) * (height / GraphicsFormatUtility.GetBlockHeight(format)) < 65536) { Debug.LogError(String.Format("SparseTexture creation failed. The minimum size in bytes of a SparseTexture is 64KB."), this); return(false); } return(true); }
internal bool ValidateSize(int width, int height, GraphicsFormat format) { bool flag = (ulong)GraphicsFormatUtility.GetBlockSize(format) * (ulong)((long)width / (long)((ulong)GraphicsFormatUtility.GetBlockWidth(format))) * (ulong)((long)height / (long)((ulong)GraphicsFormatUtility.GetBlockHeight(format))) < 65536uL; bool result; if (flag) { Debug.LogError("SparseTexture creation failed. The minimum size in bytes of a SparseTexture is 64KB.", this); result = false; } else { result = true; } return(result); }