ITensorData CustomTextureToTensorData(TextureAsTensorData texData, string name) { //By default Barracuda work on Tensor containing value in linear color space. //Here we handle custom tensor Pin from texture when tensor is to contain data in sRGB color space. //This is important for this demo as network was trained with data is sRGB color space. //Direct support for this will be added in a latter revision of Barracuda. var fn = new CustomComputeKernel(tensorToTextureSRGB, "TextureToTensor"); var tensorData = new ComputeTensorData(texData.shape, name, ComputeInfo.channelsOrder, false); fn.SetTensor("O", texData.shape, tensorData.buffer); fn.shader.SetBool("_FlipY", texData.flip == TextureAsTensorData.Flip.Y); var offsets = new int[] { 0, 0, 0, 0 }; foreach (var tex in texData.textures) { var texArr = tex as Texture2DArray; var tex3D = tex as Texture3D; var rt = tex as RenderTexture; var texDepth = 1; if (texArr) { texDepth = texArr.depth; } else if (tex3D) { texDepth = tex3D.depth; } else if (rt) { texDepth = rt.volumeDepth; } //var srcChannelMask = TextureFormatUtils.FormatToChannelMask(tex, texData.interpretPixelAsChannels); Color srcChannelMask = Color.white; fn.shader.SetTexture(fn.kernelIndex, "Xtex2D", tex); fn.shader.SetInts("_Pool", new int [] { tex.width, tex.height }); fn.shader.SetInts("_Pad", offsets); fn.shader.SetInts("_ChannelWriteMask", new [] { (int)srcChannelMask[0], (int)srcChannelMask[1], (int)srcChannelMask[2], (int)srcChannelMask[3] }); fn.Dispatch(texData.shape.width, texData.shape.height, texDepth); if (texData.interpretDepthAs == TextureAsTensorData.InterpretDepthAs.Batch) { offsets[0] += texDepth; } else if (texData.interpretDepthAs == TextureAsTensorData.InterpretDepthAs.Channels) { offsets[3] += texDepth * texData.interpretPixelAsChannels; } } return(tensorData); }
private void CustomTensorToRenderTexture(Tensor X, RenderTexture target, int batch, int fromChannel, Vector4 scale, Vector4 bias, Texture3D lut = null) { if (!internalSetup.shouldUseSRGBTensor) { X.ToRenderTexture(target, batch, fromChannel, scale, bias, lut); return; } //By default Barracuda work on Tensor containing value in linear color space. //Here we handle custom convertion from tensor to texture when tensor is in sRGB color space. //This is important for this demo as network was trained with data is sRGB color space. //Direct support for this will be added in a latter revision of Barracuda. if (!target.enableRandomWrite || !target.IsCreated()) { target.Release(); target.enableRandomWrite = true; target.Create(); } var gpuBackend = new ReferenceComputeOps(ComputeShaderSingleton.Instance.referenceKernels); var fn = new CustomComputeKernel(tensorToTextureSRGB, "TensorToTexture" + (lut == null?"NoLUT":"3DLUT")); var XonDevice = gpuBackend.Pin(X); fn.SetTensor("X", X.shape, XonDevice.buffer, XonDevice.offset); fn.shader.SetTexture(fn.kernelIndex, "Otex2D", target); fn.shader.SetVector("_Scale", scale); fn.shader.SetVector("_Bias", bias); fn.shader.SetInts("_Pad", new int[] { batch, 0, 0, fromChannel }); fn.shader.SetBool("_FlipY", true); if (lut != null) { fn.shader.SetTexture(fn.kernelIndex, "Otex3D", lut); fn.shader.SetVector("_LutParams", new Vector2(1f / lut.width, lut.width - 1f)); } fn.Dispatch(target.width, target.height, 1); }