Example #1
0
        public void Apply(  GraphicsDevice graphicsDevice, 
                            EffectParameterCollection parameters,
                            ConstantBuffer[] cbuffers )
        {
			// NOTE: We make the assumption here that the caller has
			// locked the d3dContext for us to use.
            var d3dContext = graphicsDevice._d3dContext;
            if (_pixelShader != null)
            {
                foreach (var sampler in _samplers)
                {
                    var param = parameters[sampler.parameter];
                    var texture = param.Data as Texture;
                    graphicsDevice.Textures[sampler.index] = texture;
                }

                d3dContext.PixelShader.Set(_pixelShader);
            }
            else
            {
                d3dContext.VertexShader.Set(_vertexShader);

                // Set the shader on the device so it can 
                // apply the correct input layout at draw time.
                graphicsDevice._vertexShader = this;
            }

            // Update and set the constants.
            for (var c = 0; c < _cbuffers.Length; c++)
            {
                var cb = cbuffers[_cbuffers[c]];
                cb.Apply(_vertexShader != null, c, parameters);
            }
        }
Example #2
0
 public ConstantBuffer(ConstantBuffer cloneSource)
 {
   this.GraphicsDevice = cloneSource.GraphicsDevice;
   this._name = cloneSource._name;
   this._parameters = cloneSource._parameters;
   this._offsets = cloneSource._offsets;
   this._buffer = (byte[]) cloneSource._buffer.Clone();
   this.Initialize();
 }
Example #3
0
        public ConstantBuffer(ConstantBuffer cloneSource)
        {
            GraphicsDevice = cloneSource.GraphicsDevice;

            // Share the immutable types.
            _name = cloneSource._name;
            _parameters = cloneSource._parameters;
            _offsets = cloneSource._offsets;

            // Clone the mutable types.
            _buffer = (byte[])cloneSource._buffer.Clone();
            Initialize();
        }
Example #4
0
        public unsafe void PlatformApply(GraphicsDevice device, ShaderProgram program)
        {
            // NOTE: We assume here the program has
            // already been set on the device.

            // If the program changed then lookup the
            // uniform again and apply the state.
            if (_shaderProgram != program)
            {
                var location = program.GetUniformLocation(_name);
                if (location == -1)
                    return;

                _shaderProgram = program;
                _location = location;
                _dirty = true;
            }

            // If the shader program is the same, the effect may still be different and have different values in the buffer
            if (!Object.ReferenceEquals(this, _lastConstantBufferApplied))
                _dirty = true;

            // If the buffer content hasn't changed then we're
            // done... use the previously set uniform state.
            if (!_dirty)
                return;

            fixed (byte* bytePtr = _buffer)
            {
                // TODO: We need to know the type of buffer float/int/bool
                // and cast this correctly... else it doesn't work as i guess
                // GL is checking the type of the uniform.

            #if SDL2
                device.GLDevice.glUniform4fv(
                    _location,
                    _buffer.Length / 16,
                    (IntPtr) bytePtr
                );
            #else
                GL.Uniform4(_location, _buffer.Length / 16, (float*)bytePtr);
            #endif
            }

            // Clear the dirty flag.
            _dirty = false;

            _lastConstantBufferApplied = this;
        }
Example #5
0
		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];
        }
Example #6
0
        /// <summary>
        /// Clone the source into this existing object.
        /// </summary>
        /// <remarks>
        /// Note this is not overloaded in derived classes on purpose.  This is
        /// only a reason this exists is for caching effects.
        /// </remarks>
        /// <param name="cloneSource">The source effect to clone from.</param>
        private void Clone(Effect cloneSource)
        {
            Debug.Assert(_isClone, "Cannot clone into non-cloned effect!");

            // Copy the mutable members of the effect.
            Parameters = cloneSource.Parameters.Clone();
            Techniques = cloneSource.Techniques.Clone(this);

            // Make a copy of the immutable constant buffers.
            ConstantBuffers = new ConstantBuffer[cloneSource.ConstantBuffers.Length];
            for (var i = 0; i < cloneSource.ConstantBuffers.Length; i++)
                ConstantBuffers[i] = new ConstantBuffer(cloneSource.ConstantBuffers[i]);

            // Find and set the current technique.
            for (var i = 0; i < cloneSource.Techniques.Count; i++)
            {
                if (cloneSource.Techniques[i] == cloneSource.CurrentTechnique)
                {
                    CurrentTechnique = Techniques[i];
                    break;
                }
            }

            // Take a reference to the original shader list.
            _shaders = cloneSource._shaders;
        }
Example #7
0
		private void ReadEffect (BinaryReader reader)
		{
			// Check the header to make sure the file and version is correct!
			var header = new string (reader.ReadChars (MGFXHeader.Length));
			var version = (int)reader.ReadByte ();
			if (header != MGFXHeader)
				throw new Exception ("The MGFX file is corrupt!");
            if (version != MGFXVersion)
                throw new Exception("Wrong MGFX file version!");

			var profile = reader.ReadByte ();
#if DIRECTX
            if (profile != 1)
#else
			if (profile != 0)
#endif
				throw new Exception("The MGFX effect is the wrong profile for this platform!");

			// 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];
        }
Example #8
0
 public unsafe void Apply(GraphicsDevice device, int program)
 {
   if (this._program != program)
   {
     int uniformLocation = GL.GetUniformLocation(program, this._name);
     if (uniformLocation == -1)
       return;
     this._program = program;
     this._location = uniformLocation;
     this._dirty = true;
   }
   if (!object.ReferenceEquals((object) this, (object) ConstantBuffer._lastConstantBufferApplied))
     this._dirty = true;
   if (!this._dirty)
     return;
   fixed (byte* numPtr = this._buffer)
     GL.Uniform4(this._location, this._buffer.Length / 16, (float*) numPtr);
   this._dirty = false;
   ConstantBuffer._lastConstantBufferApplied = this;
 }
Example #9
0
		public virtual void Apply (GraphicsDevice graphicsDevice,
							int program, 
							EffectParameterCollection parameters,
							ConstantBuffer[] cbuffers)
		{									
			var textures = graphicsDevice.Textures;
			var samplerStates = graphicsDevice.SamplerStates;

			if (ShaderType == ShaderType.FragmentShader) {
				// Activate the textures.
				foreach (var sampler in _samplers) {
					// Set the sampler texture slot.
					//
					// TODO: This seems like it only needs to be done once!
					//
					var loc = GL.GetUniformLocation (program, sampler.name);
					GL.Uniform1 (loc, sampler.index);

					// TODO: Fix Volume samplers!
					// (are they really broken?)
					if (sampler.type == SamplerType.SamplerVolume)
						throw new NotImplementedException ();

					Texture tex = null;
					if (sampler.parameter >= 0) {
						var textureParameter = parameters [sampler.parameter];
						tex = textureParameter.Data as Texture;
					}

					if (tex == null) {
						//texutre 0 will be set in drawbatch :/
						if (sampler.index == 0)
							continue;

						//are smapler indexes always normal texture indexes?
						tex = (Texture)textures [sampler.index];
					}

					if (tex != null) {
						tex.glTextureUnit = ((TextureUnit)((int)TextureUnit.Texture0 + sampler.index));
						tex.Activate ();						
						samplerStates [sampler.index].Activate (tex.glTarget, tex.LevelCount > 1);
					}
				}
			}

			// Update and set the constants.
			for (var c = 0; c < _cbuffers.Length; c++) {
				var cb = cbuffers [_cbuffers [c]];
				cb.Apply (program, parameters);
			}

			if (ShaderType == ShaderType.VertexShader) {
				// Apply vertex shader fix:
				// The following two lines are appended to the end of vertex shaders
				// to account for rendering differences between OpenGL and DirectX:
				//
				// gl_Position.y = gl_Position.y * posFixup.y;
				// gl_Position.xy += posFixup.zw * gl_Position.ww;
				//
				// (the following paraphrased from wine, wined3d/state.c and wined3d/glsl_shader.c)
				//
				// - We need to flip along the y-axis in case of offscreen rendering.
				// - D3D coordinates refer to pixel centers while GL coordinates refer
				//   to pixel corners.
				// - D3D has a top-left filling convention. We need to maintain this
				//   even after the y-flip mentioned above.
				// In order to handle the last two points, we translate by
				// (63.0 / 128.0) / VPw and (63.0 / 128.0) / VPh. This is equivalent to
				// translating slightly less than half a pixel. We want the difference to
				// be large enough that it doesn't get lost due to rounding inside the
				// driver, but small enough to prevent it from interfering with any
				// anti-aliasing.
				//
				// OpenGL coordinates specify the center of the pixel while d3d coords specify
				// the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
				// 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
				// contains 1.0 to allow a mad.
	
				_posFixup [0] = 1.0f;
				_posFixup [1] = 1.0f;
				_posFixup [2] = (63.0f / 64.0f) / graphicsDevice.Viewport.Width;
				_posFixup [3] = -(63.0f / 64.0f) / graphicsDevice.Viewport.Height;
				//If we have a render target bound (rendering offscreen)
				if (graphicsDevice.GetRenderTargets ().Length > 0) {
					//flip vertically
					_posFixup [1] *= -1.0f;
					_posFixup [3] *= -1.0f;
				}
				var posFixupLoc = GL.GetUniformLocation (program, "posFixup"); // TODO: Look this up on link!
				GL.Uniform4 (posFixupLoc, 1, _posFixup);
			}
		}
Example #10
0
        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];
        }
Example #11
0
 private void ReadEffect(BinaryReader reader)
 {
   if (new string(reader.ReadChars("MGFX".Length)) != "MGFX")
     throw new Exception("The MGFX file is corrupt!");
   this.version = (int) reader.ReadByte();
   if (this.version < 4)
     throw new Exception("Unsupported MGFX file version!");
   if ((int) reader.ReadByte() != 0)
     throw new Exception("The MGFX effect is the wrong profile for this platform!");
   int length = (int) reader.ReadByte();
   this.ConstantBuffers = new ConstantBuffer[length];
   for (int index1 = 0; index1 < length; ++index1)
   {
     string name = reader.ReadString();
     int sizeInBytes = (int) reader.ReadInt16();
     int[] parameterIndexes = new int[(int) reader.ReadByte()];
     int[] parameterOffsets = new int[parameterIndexes.Length];
     for (int index2 = 0; index2 < parameterIndexes.Length; ++index2)
     {
       parameterIndexes[index2] = (int) reader.ReadByte();
       parameterOffsets[index2] = (int) reader.ReadUInt16();
     }
     ConstantBuffer constantBuffer = new ConstantBuffer(this.GraphicsDevice, sizeInBytes, parameterIndexes, parameterOffsets, name);
     this.ConstantBuffers[index1] = constantBuffer;
   }
   this._shaderList = new List<Shader>();
   int num1 = (int) reader.ReadByte();
   for (int index = 0; index < num1; ++index)
     this._shaderList.Add(new Shader(this.GraphicsDevice, reader));
   this.Parameters = this.ReadParameters(reader);
   this.Techniques = new EffectTechniqueCollection();
   int num2 = (int) reader.ReadByte();
   for (int index = 0; index < num2; ++index)
   {
     string name = reader.ReadString();
     EffectAnnotationCollection annotations = Effect.ReadAnnotations(reader);
     EffectPassCollection passes = Effect.ReadPasses(reader, this, this._shaderList);
     this.Techniques.Add(new EffectTechnique(this, name, passes, annotations));
   }
   this.CurrentTechnique = this.Techniques[0];
 }
Example #12
0
        private void ReadEffect(BinaryReader reader)
        {
            // Check the header to make sure the file and version is correct!
            var header  = new string (reader.ReadChars(MGFXHeader.Length));
            var version = (int)reader.ReadByte();

            if (header != MGFXHeader)
            {
                throw new Exception("The MGFX file is corrupt!");
            }
            if (version != MGFXVersion)
            {
                throw new Exception("Wrong MGFX file version!");
            }

            var profile = reader.ReadByte();

#if DIRECTX
            if (profile != 1)
#else
            if (profile != 0)
#endif
            { throw new Exception("The MGFX effect is the wrong profile for this platform!"); }

            // 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.
                int[] parameters = new int[reader.ReadByte()];
                int[] 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.
            _shaderList = new List <Shader>();
            var shaders = (int)reader.ReadByte();
            for (var s = 0; s < shaders; s++)
            {
                var shader = new Shader(GraphicsDevice, reader);
                _shaderList.Add(shader);
            }

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

            // Read the techniques.
            Techniques = new EffectTechniqueCollection();
            var techniques = (int)reader.ReadByte();
            for (var t = 0; t < techniques; t++)
            {
                var name = reader.ReadString();

                var annotations = ReadAnnotations(reader);

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

                var technique = new EffectTechnique(this, name, passes, annotations);
                Techniques.Add(technique);
            }

            CurrentTechnique = Techniques[0];
        }