Beispiel #1
0
        // TODO@: expose now that Download necesarrily goes via the gpu (compute/pixel) ?
        private float[] TextureToTensorDataCache(TensorShape shape)
        {
            float[] tensorDataCache = new float[shape.length];
            bool    flipY           = flip == Flip.Y;

            int batchOffset   = 0;
            int channelOffset = 0;

            foreach (var tex in textures)
            {
                var channelWriteMask = TextureFormatUtils.FormatToChannelMask(tex, interpretPixelAsChannels);
                var channelReadMap   = TextureFormatUtils.FormatToChannelReadMap(tex, interpretPixelAsChannels);

                FillCacheFromTexture(tensorDataCache, tex, batchOffset, channelOffset, channelWriteMask, channelReadMap,
                                     flipY, scale, bias, shape);

                if (interpretDepthAs == InterpretDepthAs.Batch)
                {
                    batchOffset += 1;
                }
                else if (interpretDepthAs == InterpretDepthAs.Channels)
                {
                    channelOffset += interpretPixelAsChannels;
                }
            }

            return(tensorDataCache);
        }
Beispiel #2
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);
        }
        internal void OnPostprocessTexture(Texture2D tex)
        {
            TextureImporter impl = assetImporter as TextureImporter;

            if (impl)
            {
                string ext = Path.GetExtension(assetPath);
                if (string.IsNullOrEmpty(ext) || !SUPPORT_EXTENTIONS.Contains(ext))
                {
                    AssetDatabase.DeleteAsset(assetPath);
                    EditorUtility.DisplayDialog("error",
                                                "the pic @ " + assetPath + " deleted, the game support png and tga only", "OK");
                    return;
                }
                if (TextureFormatUtils.isReimporting)
                {
                    return;
                }
                if (impl && impl.textureType == TextureImporterType.Sprite)
                {
                    TextureFormatUtils.SetUITexture(impl);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Create `TextureAsTensorData` from supplied `textures`
        /// </summary>
        /// <param name="textures">backing textures</param>
        /// <param name="interpretPixelAsChannels">interpret pixel as channels</param>
        /// <param name="flip">flip</param>
        /// <param name="depthAs">depth as</param>
        /// <param name="colorAs">color as</param>
        /// <param name="scale">multiplies `scale` to texture values</param>
        /// <param name="bias">substracts `bias` from texture values</param>
        /// <exception cref="ArgumentException">thrown if textures array is empty or texture types are different</exception>
        /// <exception cref="InvalidOperationException">thrown if unsupported texture type is supplied</exception>
        public TextureAsTensorData(Texture[] textures,
                                   Flip flip, InterpretDepthAs depthAs, InterpretColorAs colorAs, Vector4 scale, Vector4 bias,
                                   int interpretPixelAsChannels)
        {
            if (textures.Length < 1)
            {
                throw new ArgumentException("Textures array must be non empty");
            }

            if (interpretPixelAsChannels < 0)
            {
                interpretPixelAsChannels = TextureFormatUtils.FormatToChannelCount(textures[0]);

                // check that all textures have the same number of channels
                foreach (var tex in textures)
                {
                    if (interpretPixelAsChannels != TextureFormatUtils.FormatToChannelCount(tex))
                    {
                        throw new ArgumentException("All textures must have the same number of channels");
                    }
                }
            }

            m_InterpretPixelAsChannels = interpretPixelAsChannels;
            m_InterpretDepthAs         = depthAs;
            m_InterpretColorAs         = colorAs;
            m_Flip = flip;

            m_scale = scale;
            m_bias  = bias;

            var width  = textures[0].width;
            var height = textures[0].height;

            var totalDepth = 0;

            foreach (var tex in textures)
            {
                if (tex.width != width || tex.height != height)
                {
                    throw new ArgumentException("All textures must have the same width and height dimensions");
                }

                var tex2D  = tex as Texture2D;
                var texArr = tex as Texture2DArray;
                var tex3D  = tex as Texture3D;
                var rt     = tex as RenderTexture;
                if (tex2D)
                {
                    totalDepth += 1;
                }
                else if (texArr)
                {
                    totalDepth += texArr.depth;
                }
                else if (tex3D)
                {
                    totalDepth += tex3D.depth;
                }
                else if (rt)
                {
                    totalDepth += rt.volumeDepth;
                }
                else
                {
                    throw new InvalidOperationException("Unsupported texture type");
                }
            }

            m_Textures = textures;

            int batch    = 1;
            int channels = interpretPixelAsChannels;

            if (m_InterpretDepthAs == InterpretDepthAs.Batch)
            {
                batch *= totalDepth;
            }
            else if (m_InterpretDepthAs == InterpretDepthAs.Channels)
            {
                channels *= totalDepth;
            }

            m_Shape = new TensorShape(batch, height, width, channels);
        }