static void FillInitData(Resource texture, int width, int height, int depth, int mipCount, int arraySize, Format format, int maxsize, int bitSize, byte[] bitData, int offset) { int NumBytes = 0; int RowBytes = 0; int NumRows = 0; byte[] pSrcBits = bitData; byte[] pEndBits = bitData;// + bitSize; int index = 0; int k = offset; for (int j = 0; j < arraySize; j++) { int w = width; int h = height; int d = depth; for (int i = 0; i < mipCount; i++) { GetSurfaceInfo(w, h, format, out NumBytes, out RowBytes, out NumRows); GCHandle handle = GCHandle.Alloc(bitData, GCHandleType.Pinned); IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(bitData, k); texture.WriteToSubresource(index, null, ptr, RowBytes, NumBytes); handle.Free(); index++; k += NumBytes * d; w = w >> 1; h = h >> 1; d = d >> 1; if (w == 0) { w = 1; } if (h == 0) { h = 1; } if (d == 0) { d = 1; } } } }
private void BuildRandomVectorTexture(GraphicsCommandList cmdList) { var texDesc = new ResourceDescription { Dimension = ResourceDimension.Texture2D, Width = 256, Height = 256, DepthOrArraySize = 1, MipLevels = 1, Format = Format.R8G8B8A8_UNorm, SampleDescription = new SampleDescription(1, 0), Layout = TextureLayout.Unknown, Flags = ResourceFlags.None }; _randomVectorMap = _device.CreateCommittedResource( new HeapProperties(CpuPageProperty.WriteBack, MemoryPool.L0), HeapFlags.None, texDesc, ResourceStates.GenericRead); // // In order to copy CPU memory data into our default buffer, we need to create // an intermediate upload heap. // var initData = new Color[256 * 256]; for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { // Random vector in [0,1]. We will decompress in shader to [-1,1]. initData[i * 256 + j] = new Color( MathHelper.Randf(), MathHelper.Randf(), MathHelper.Randf(), 0.0f); } } int rowPitch = Utilities.SizeOf <Color>() * 256; int slicePitch = rowPitch * 256; Utilities.Pin(initData, ptr => _randomVectorMap.WriteToSubresource(0, null, ptr, rowPitch, slicePitch)); }