Beispiel #1
0
        public unsafe Effect(GraphicsDevice graphicsDevice, ReadOnlySpan <byte> effectCode)
            : this(graphicsDevice)
        {
            // By default we currently cache all unique byte streams
            // and use cloning to populate the effect with parameters,
            // techniques, and passes.
            //
            // This means all the immutable types in an effect:
            //
            //  - Shaders
            //  - Annotations
            //  - Names
            //  - State Objects
            //
            // Are shared for every instance of an effect while the
            // parameter values and constant buffers are copied.
            //
            // This might need to change slightly if/when we support
            // shared constant buffers as 'new' should return unique
            // effects without any shared instance state.

            //Read the header
            MGFXHeader header     = ReadHeader(effectCode);
            var        effectKey  = header.EffectKey;
            int        headerSize = header.HeaderSize;

            // First look for it in the cache.
            if (!graphicsDevice.EffectCache.TryGetValue(effectKey, out var cloneSource))
            {
                // Create one.
                fixed(byte *effectCodePtr = effectCode[headerSize..])
Beispiel #2
0
        public Effect(GraphicsDevice graphicsDevice, byte[] effectCode)
            : this(graphicsDevice)
        {
            // By default we currently cache all unique byte streams
            // and use cloning to populate the effect with parameters,
            // techniques, and passes.
            //
            // This means all the immutable types in an effect:
            //
            //  - Shaders
            //  - Annotations
            //  - Names
            //  - State Objects
            //
            // Are shared for every instance of an effect while the
            // parameter values and constant buffers are copied.
            //
            // This might need to change slightly if/when we support
            // shared constant buffers as 'new' should return unique
            // effects without any shared instance state.

#if PSM
            var effectKey  = MonoGame.Utilities.Hash.ComputeHash(effectCode);
            int headerSize = 0;
#else
            //Read the header
            MGFXHeader header     = ReadHeader(effectCode);
            var        effectKey  = header.EffectKey;
            int        headerSize = header.HeaderSize;
#endif

            // First look for it in the cache.
            //
            Effect cloneSource;
            if (!EffectCache.TryGetValue(effectKey, out cloneSource))
            {
                using (var stream = new MemoryStream(effectCode, headerSize, effectCode.Length - headerSize, false))
                    using (var reader = new BinaryReader(stream))
                    {
                        // Create one.
                        cloneSource = new Effect(graphicsDevice);
                        cloneSource.ReadEffect(reader);

                        // Cache the effect for later in its original unmodified state.
                        EffectCache.Add(effectKey, cloneSource);
                    }
            }

            // Clone it.
            _isClone = true;
            Clone(cloneSource);
        }
Beispiel #3
0
        public Effect(GraphicsDevice graphicsDevice, byte[] effectCode, int index, int count) : this(graphicsDevice)
        {
            MGFXHeader header     = ReadHeader(effectCode, index);
            var        effectKey  = header.EffectKey;
            var        headerSize = header.HeaderSize;

            Effect cloneSource;
            if (!graphicsDevice.EffectCache.TryGetValue(effectKey, out cloneSource))
            {
                using (var stream = new MemoryStream(effectCode, index + headerSize, count - headerSize, false))
                    using (var reader = new BinaryReader(stream))
                    {
                        cloneSource = new Effect(graphicsDevice);
                        cloneSource.ReadEffect(reader);

                        graphicsDevice.EffectCache.Add(effectKey, cloneSource);
                    }
            }

            _isClone = true;
            Clone(cloneSource);
        }