Microsoft.Xna.Framework.Graphics.Effect.ReadEffect C# (CSharp) Method

ReadEffect() private method

private ReadEffect ( BinaryReader reader ) : void
reader BinaryReader
return void
		private void ReadEffect (BinaryReader reader)
		{
			// TODO: Maybe we should be reading in a string 
			// table here to save some bytes in the file.

			// Read in all the constant buffers.
			var buffers = (int)reader.ReadByte ();
			ConstantBuffers = new ConstantBuffer[buffers];
			for (var c = 0; c < buffers; c++) 
            {
				
#if OPENGL
				string name = reader.ReadString ();               
#else
				string name = null;
#endif

				// Create the backing system memory buffer.
				var sizeInBytes = (int)reader.ReadInt16 ();

				// Read the parameter index values.
				var parameters = new int[reader.ReadByte ()];
				var offsets = new int[parameters.Length];
				for (var i = 0; i < parameters.Length; i++) 
                {
					parameters [i] = (int)reader.ReadByte ();
					offsets [i] = (int)reader.ReadUInt16 ();
				}

                var buffer = new ConstantBuffer(GraphicsDevice,
				                                sizeInBytes,
				                                parameters,
				                                offsets,
				                                name);
                ConstantBuffers[c] = buffer;
            }

            // Read in all the shader objects.
            var shaders = (int)reader.ReadByte();
            _shaders = new Shader[shaders];
            for (var s = 0; s < shaders; s++)
                _shaders[s] = new Shader(GraphicsDevice, reader);

            // Read in the parameters.
            Parameters = ReadParameters(reader);

            // Read the techniques.
            var techniqueCount = (int)reader.ReadByte();
            var techniques = new EffectTechnique[techniqueCount];
            for (var t = 0; t < techniqueCount; t++)
            {
                var name = reader.ReadString();

                var annotations = ReadAnnotations(reader);

                var passes = ReadPasses(reader, this, _shaders);

                techniques[t] = new EffectTechnique(this, name, passes, annotations);
            }

            Techniques = new EffectTechniqueCollection(techniques);
            CurrentTechnique = Techniques[0];
        }

Usage Example

Ejemplo n.º 1
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);
        }
All Usage Examples Of Microsoft.Xna.Framework.Graphics.Effect::ReadEffect