Ejemplo n.º 1
0
        // ---------------------------------------------------------------------------------

        internal ITensorData TextureToTensorData(TextureAsTensorData texData, string name)
        {
            var tensorData = new TextureTensorData(texData.shape, name, false);

            Material material = new Material(PixelShaderSingleton.Instance.FindShader("Barracuda/TextureToTensor"));

            material.SetVector("OdeclShape", new Vector4(texData.shape.batch, texData.shape.height, texData.shape.width, texData.shape.channels));

            material.SetInt("_FlipY", texData.flip == TextureAsTensorData.Flip.Y ? 1 : 0);
            material.SetVector("_Scale", texData.scale);
            material.SetVector("_Bias", texData.bias);

            Vector4 offsets = Vector4.zero;

            foreach (var tex in texData.textures)
            {
                var texArr = tex as Texture2DArray;
                var rt     = tex as RenderTexture;

                var texDepth = 1;
                if (texArr)
                {
                    texDepth = texArr.depth;
                }
                else if (rt)
                {
                    texDepth = rt.volumeDepth;
                }

                material.SetTexture("Xtex2D", tex);
                material.SetVector("_Pool", new Vector2(tex.width, tex.height));
                material.SetVector("_Pad", offsets);

                var channelWriteMask = TextureFormatUtils.FormatToChannelMask(tex, texData.interpretPixelAsChannels);
                var channelReadMap   = TextureFormatUtils.FormatToChannelReadMap(tex, texData.interpretPixelAsChannels);
                material.SetVector("_ChannelWriteMask", new Vector4(channelWriteMask[0], channelWriteMask[1], channelWriteMask[2], channelWriteMask[3]));
                material.SetVector("_ChannelReadMap", new Vector4(channelReadMap[0], channelReadMap[1], channelReadMap[2], channelReadMap[3]));

                Graphics.Blit(null, tensorData.bufferAsTexture, material);

                if (texData.interpretDepthAs == TextureAsTensorData.InterpretDepthAs.Batch)
                {
                    offsets[0] += texDepth;
                }
                else if (texData.interpretDepthAs == TextureAsTensorData.InterpretDepthAs.Channels)
                {
                    offsets[3] += texDepth * texData.interpretPixelAsChannels;
                }
            }

            return(tensorData);
        }
        /// <summary>
        /// Create a Tensor from multiple texture, shape is [srcTextures.length, texture.height, texture.width, `channels=3`]
        /// All textures must be of the same size and dimension.
        /// </summary>
        public Tensor(UnityEngine.Texture[] srcTextures, int channels = 3, string n = "")
        {
            name = n;
            var tensorData = new TextureAsTensorData(srcTextures, channels);

            //;;UnityEngine.Debug.Log("Tensor::Tensor " + n + " " + tensorData.shape + " [TEX] " + srcTextures);
            shape = tensorData.shape;
            Assert.IsTrue(tensorData.GetMaxCount() >= length);
            m_TensorOnDevice  = tensorData;
            m_TensorAllocator = null;
            m_Cache           = null;
            m_CacheIsDirty    = false;
        }