Example #1
0
        /// <summary>
        /// Compiles the ShaderProgram. This is done automatically when loading the ShaderProgram
        /// or when binding it.
        /// </summary>
        public void Compile()
        {
            if (this.native == null)
            {
                this.native = DualityApp.GraphicsBackend.CreateShaderProgram();
            }

            // Assure both shaders are compiled
            this.CompileIfRequired(this.vert.Res);
            this.CompileIfRequired(this.frag.Res);

            // Load the program with both shaders attached
            INativeShaderPart nativeVert = this.vert.Res != null ? this.vert.Res.Native : null;
            INativeShaderPart nativeFrag = this.frag.Res != null ? this.frag.Res.Native : null;

            try {
                this.native.LoadProgram(nativeVert, nativeFrag);
                this.fields = this.native.GetFields();
            } catch (Exception e) {
                this.fields = new ShaderFieldInfo[0];
                Console.WriteLine("Error loading ShaderProgram {0}:{2}{1}", this.FullName, /*Log.Exception(*/ e /*)*/, Environment.NewLine);
            }

            // Even if we failed, we tried to compile it. Don't do it again and again.
            this.compiled = true;
        }
Example #2
0
        /// <summary>
        /// Compiles the internal shader program of this <see cref="DrawTechnique"/>. This is
        /// done automatically on load and only needs to be invoked manually when the technique
        /// or one of its shader dependencies changed.
        /// </summary>
        public void Compile()
        {
            if (this.nativeShader == null)
            {
                this.nativeShader = DualityApp.GraphicsBackend.CreateShaderProgram();
            }

            // Assure both shaders are compiled
            this.CompileIfRequired(this.vertexShader.Res);
            this.CompileIfRequired(this.fragmentShader.Res);

            // Load the program with both shaders attached
            INativeShaderPart nativeVert = this.vertexShader.Res != null ? this.vertexShader.Res.Native : null;
            INativeShaderPart nativeFrag = this.fragmentShader.Res != null ? this.fragmentShader.Res.Native : null;

            // Load the program with all shader parts attached
            try {
                this.nativeShader.LoadProgram(nativeVert, nativeFrag);
                this.shaderFields = this.nativeShader.GetFields();
            } catch (Exception e) {
                this.shaderFields = new ShaderFieldInfo[0];
                Console.WriteLine("Failed to compile DrawTechnique:{1}{0}", e, Environment.NewLine);
            }

            // Even if we failed, we tried to compile it. Don't do it again and again.
            this.compiled = true;
        }
Example #3
0
 protected override void OnDisposing(bool manually)
 {
     base.OnDisposing(manually);
     if (this.nativeShader != null)
     {
         this.nativeShader.Dispose();
         this.nativeShader = null;
     }
 }
Example #4
0
        /// <summary>
        /// Compiles the ShaderProgram. This is done automatically when loading the ShaderProgram
        /// or when binding it.
        /// </summary>
        /// <param name="force">If true, the program is recompiled even if it already was compiled before.</param>
        public void Compile(bool force = false)
        {
            if (!force && this.compiled)
            {
                return;
            }
            if (this.native == null)
            {
                this.native = DualityApp.GraphicsBackend.CreateShaderProgram();
            }

            // Assure both shaders are compiled
            if (this.vert.IsAvailable)
            {
                this.vert.Res.Compile();
            }
            if (this.frag.IsAvailable)
            {
                this.frag.Res.Compile();
            }

            // Load the program with both shaders attached
            INativeShaderPart nativeVert = this.vert.Res != null ? this.vert.Res.Native : null;
            INativeShaderPart nativeFrag = this.frag.Res != null ? this.frag.Res.Native : null;

            try
            {
                this.native.LoadProgram(nativeVert, nativeFrag);
            }
            catch (Exception e)
            {
                Log.Core.WriteError("Error loading ShaderProgram {0}:{2}{1}", this.FullName, Log.Exception(e), Environment.NewLine);
            }

            // Determine actual variable locations
            this.fields = this.native.GetFields();

            this.compiled = true;
        }
Example #5
0
 protected override void OnDisposing(bool manually)
 {
     base.OnDisposing(manually);
     if (this.native != null)
     {
         this.native.Dispose();
         this.native = null;
     }
 }
Example #6
0
        /// <summary>
        /// Compiles the ShaderProgram. This is done automatically when loading the ShaderProgram
        /// or when binding it.
        /// </summary>
        /// <param name="force">If true, the program is recompiled even if it already was compiled before.</param>
        public void Compile(bool force = false)
        {
            if (!force && this.compiled) return;
            if (this.native == null) this.native = DualityApp.GraphicsBackend.CreateShaderProgram();

            // Assure both shaders are compiled
            if (this.vert.IsAvailable) this.vert.Res.Compile();
            if (this.frag.IsAvailable) this.frag.Res.Compile();

            // Load the program with both shaders attached
            INativeShaderPart nativeVert = this.vert.Res != null ? this.vert.Res.Native : null;
            INativeShaderPart nativeFrag = this.frag.Res != null ? this.frag.Res.Native : null;
            try
            {
                this.native.LoadProgram(nativeVert, nativeFrag);
            }
            catch (Exception e)
            {
                Log.Core.WriteError("Error loading ShaderProgram {0}:{2}{1}", this.FullName, Log.Exception(e), Environment.NewLine);
            }

            // Determine actual variable locations
            this.fields = this.native.GetFields();

            this.compiled = true;
        }
Example #7
0
        /// <summary>
        /// Compiles the internal shader program of this <see cref="DrawTechnique"/>. This is
        /// done automatically on load and only needs to be invoked manually when the technique
        /// or one of its shader dependencies changed.
        /// </summary>
        public void Compile()
        {
            Logs.Core.Write("Compiling DrawTechnique '{0}'...", this.FullName);
            Logs.Core.PushIndent();

            if (this.nativeShader == null)
            {
                this.nativeShader = DualityApp.GraphicsBackend.CreateShaderProgram();
            }

            // Create a list of all shader parts that we'll be linking
            List <Shader> parts = new List <Shader>();

            parts.Add(this.vertexShader.Res ?? VertexShader.Minimal.Res);
            parts.Add(this.fragmentShader.Res ?? FragmentShader.Minimal.Res);

            // Ensure all shader parts are compiled
            List <INativeShaderPart> nativeParts = new List <INativeShaderPart>();

            foreach (Shader part in parts)
            {
                if (!part.Compiled)
                {
                    part.Compile();
                }
                nativeParts.Add(part.Native);
            }

            // Gather shader field declarations from all shader parts
            Dictionary <string, ShaderFieldInfo> fieldMap = new Dictionary <string, ShaderFieldInfo>();

            foreach (Shader part in parts)
            {
                foreach (ShaderFieldInfo field in part.DeclaredFields)
                {
                    fieldMap[field.Name] = field;
                }
            }

            // Load the program with all shader parts attached
            try
            {
                this.shaderFields = fieldMap.Values.ToArray();
                this.nativeShader.LoadProgram(nativeParts, this.shaderFields);

                // Validate that we have at least one attribute in the shader. Warn otherwise.
                if (!this.shaderFields.Any(f => f.Scope == ShaderFieldScope.Attribute))
                {
                    Logs.Core.WriteWarning("The shader doesn't seem to define any vertex attributes. Is this intended?");
                }
            }
            catch (Exception e)
            {
                this.shaderFields = new ShaderFieldInfo[0];
                Logs.Core.WriteError("Failed to compile DrawTechnique:{1}{0}", LogFormat.Exception(e), Environment.NewLine);
            }

            // Even if we failed, we tried to compile it. Don't do it again and again.
            this.compiled = true;
            Logs.Core.PopIndent();
        }
Example #8
0
		/// <summary>
		/// Compiles the ShaderProgram. This is done automatically when loading the ShaderProgram
		/// or when binding it.
		/// </summary>
		public void Compile()
		{
			if (this.native == null)
				this.native = DualityApp.GraphicsBackend.CreateShaderProgram();

			// Assure both shaders are compiled
			this.CompileIfRequired(this.vert.Res);
			this.CompileIfRequired(this.frag.Res);

			// Load the program with both shaders attached
			INativeShaderPart nativeVert = this.vert.Res != null ? this.vert.Res.Native : null;
			INativeShaderPart nativeFrag = this.frag.Res != null ? this.frag.Res.Native : null;
			try
			{
				this.native.LoadProgram(nativeVert, nativeFrag);
				this.fields = this.native.GetFields();
			}
			catch (Exception e)
			{
				this.fields = new ShaderFieldInfo[0];
				Log.Core.WriteError("Error loading ShaderProgram {0}:{2}{1}", this.FullName, Log.Exception(e), Environment.NewLine);
			}

			// Even if we failed, we tried to compile it. Don't do it again and again.
			this.compiled = true;
		}