private void ActionShaderCreateProgram_Create(FragmentShader frag, VertexShader vert)
		{
			AbstractShader refShader = (vert != null) ? (AbstractShader)vert : (AbstractShader)frag;

			string nameTemp = refShader.Name;
			string dirTemp = Path.GetDirectoryName(refShader.Path);
			if (nameTemp.Contains("Shader"))
				nameTemp = nameTemp.Replace("Shader", "Program");
			else if (nameTemp.Contains("Shader"))
				nameTemp = nameTemp.Replace("shader", "program");
			else
				nameTemp += "Program";

			string programPath = PathHelper.GetFreePath(Path.Combine(dirTemp, nameTemp), ShaderProgram.FileExt);
			ShaderProgram program = new ShaderProgram(vert, frag);
			program.Save(programPath);
		}
Ejemplo n.º 2
0
 /// <summary>
 /// Resets the OpenGL rendering state after finishing DrawTechnique-Setups. Only call this when there are no more
 /// DrawTechniques to follow directly.
 /// </summary>
 public void FinishRendering()
 {
     this.SetupBlendType(BlendMode.Reset);
     ShaderProgram.Bind(ContentRef <ShaderProgram> .Null);
     Texture.ResetBinding();
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Binds a ShaderProgram in order to use it.
        /// </summary>
        /// <param name="prog">The ShaderProgram to be bound.</param>
        public static void Bind(ContentRef<ShaderProgram> prog)
        {
            ShaderProgram progRes = prog.IsExplicitNull ? null : prog.Res;
            if (curBound == progRes) return;

            if (progRes == null)
            {
                GL.UseProgram(0);
                curBound = null;
            }
            else
            {
                if (!progRes.compiled) progRes.Compile();

                if (progRes.glProgramId == 0)	throw new ArgumentException("Specified shader program has no valid OpenGL program Id! Maybe it hasn't been loaded / initialized properly?", "prog");
                if (progRes.Disposed)			throw new ArgumentException("Specified shader program has already been deleted!", "prog");

                GL.UseProgram(progRes.glProgramId);
                curBound = progRes;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sets up the appropriate OpenGL rendering state for this DrawTechnique.
        /// </summary>
        /// <param name="lastTechnique">The last DrawTechnique that has been set up. This parameter is optional, but
        /// specifying it will increase performance by reducing redundant state changes.</param>
        /// <param name="textures">A set of <see cref="Duality.Resources.Texture">Textures</see> to use.</param>
        /// <param name="uniforms">A set of <see cref="Duality.Resources.ShaderVarInfo">uniform values</see> to apply.</param>
        public void SetupForRendering(IDrawDevice device, BatchInfo material, DrawTechnique lastTechnique)
        {
            // Prepare Rendering
            if (this.NeedsPreparation)
            {
                // Clone the material, if not done yet due to vertex preprocessing
                if (!this.NeedsPreprocess)
                {
                    material = new BatchInfo(material);
                }
                this.PrepareRendering(device, material);
            }

            // Setup BlendType
            if (lastTechnique == null || this.blendType != lastTechnique.blendType)
            {
                this.SetupBlendType(this.blendType, device.DepthWrite);
            }

            // Bind Shader
            ContentRef <ShaderProgram> selShader = this.SelectShader();

            if (lastTechnique == null || selShader.Res != lastTechnique.shader.Res)
            {
                ShaderProgram.Bind(selShader);
            }

            // Setup shader data
            if (selShader.IsAvailable)
            {
                ShaderVarInfo[] varInfo = selShader.Res.VarInfo;

                // Setup sampler bindings automatically
                int curSamplerIndex = 0;
                if (material.Textures != null)
                {
                    for (int i = 0; i < varInfo.Length; i++)
                    {
                        if (varInfo[i].glVarLoc == -1)
                        {
                            continue;
                        }
                        if (varInfo[i].type != ShaderVarType.Sampler2D)
                        {
                            continue;
                        }

                        // Bind Texture
                        ContentRef <Texture> texRef = material.GetTexture(varInfo[i].name);
                        Texture.Bind(texRef, curSamplerIndex);
                        GL.Uniform1(varInfo[i].glVarLoc, curSamplerIndex);

                        curSamplerIndex++;
                    }
                }
                Texture.ResetBinding(curSamplerIndex);

                // Transfer uniform data from material to actual shader
                if (material.Uniforms != null)
                {
                    for (int i = 0; i < varInfo.Length; i++)
                    {
                        if (varInfo[i].glVarLoc == -1)
                        {
                            continue;
                        }
                        float[] data = material.GetUniform(varInfo[i].name);
                        if (data == null)
                        {
                            continue;
                        }
                        varInfo[i].SetupUniform(data);
                    }
                }
            }
            // Setup fixed function data
            else
            {
                // Fixed function texture binding
                if (material.Textures != null)
                {
                    int samplerIndex = 0;
                    foreach (var pair in material.Textures)
                    {
                        Texture.Bind(pair.Value, samplerIndex);
                        samplerIndex++;
                    }
                    Texture.ResetBinding(samplerIndex);
                }
                else
                {
                    Texture.ResetBinding();
                }
            }
        }