ARBGpuProgram.cs :  » Game » RealmForge » Axiom » RenderSystems » OpenGL » ARB » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » RealmForge 
RealmForge » Axiom » RenderSystems » OpenGL » ARB » ARBGpuProgram.cs
using System;
using System.Runtime.InteropServices;
using System.Text;
using Axiom.Graphics;
using Axiom.RenderSystems.OpenGL;
using Tao.OpenGl;

namespace Axiom.RenderSystems.OpenGL.ARB{
  /// <summary>
  /// Summary description for ARBGpuProgram.
  /// </summary>
  public class ARBGpuProgram : GLGpuProgram {
        #region Constructor

        public ARBGpuProgram(string name, GpuProgramType type, string syntaxCode)
            : base(name, type, syntaxCode) {

            // set the type of program for ARB
            programType = (type == GpuProgramType.Vertex) ? Gl.GL_VERTEX_PROGRAM_ARB : Gl.GL_FRAGMENT_PROGRAM_ARB;

            // generate a new program
            Gl.glGenProgramsARB(1, out programId);
        }

        #endregion Constructor

        #region Implementation of GpuProgram

        /// <summary>
        ///     Load Assembler gpu program source.
        /// </summary>
        protected override void LoadFromSource() {
            Gl.glBindProgramARB(programType, programId);

            // MONO: Cannot compile programs when passing in the string as is for whatever reason.
            // would get "Invalid vertex program header", which I assume means the source got mangled along the way
            byte[] bytes = Encoding.ASCII.GetBytes(source);
            IntPtr sourcePtr = Marshal.UnsafeAddrOfPinnedArrayElement(bytes, 0);
            Gl.glProgramStringARB(programType, Gl.GL_PROGRAM_FORMAT_ASCII_ARB, source.Length, sourcePtr);

            // check for any errors
            if(Gl.glGetError() == Gl.GL_INVALID_OPERATION) {
                int pos;
                string error;

                Gl.glGetIntegerv(Gl.GL_PROGRAM_ERROR_POSITION_ARB, out pos);
                error = Marshal.PtrToStringAnsi(Gl.glGetString(Gl.GL_PROGRAM_ERROR_STRING_ARB));

                throw new Exception(string.Format("Error on line {0} in program '{1}'\nError: {2}", pos, name, error));
            }
        }

        /// <summary>
        ///     Unload GL gpu programs.
        /// </summary>
        public override void Unload() {
            base.Unload();

            if (isLoaded) {
                Gl.glDeleteProgramsARB(1, ref programId);

                isLoaded = false;
            }
        }

        #endregion Implementation of GpuProgram

        #region Implementation of GLGpuProgram

        public override void Bind() {
            Gl.glEnable(programType);
            Gl.glBindProgramARB(programType, programId);
        }

        public override void Unbind() {
            Gl.glBindProgramARB(programType, 0);
            Gl.glDisable(programType);
        }

        public override void BindParameters(GpuProgramParameters parms) {
            if(parms.HasFloatConstants) {
                for(int index = 0; index < parms.FloatConstantCount; index++) {
                    GpuProgramParameters.FloatConstantEntry entry = parms.GetFloatConstant(index);

          if(entry.isSet) {
            // MONO: the 4fv version does not work
                        float[] vals = entry.val;
                        Gl.glProgramLocalParameter4fARB(programType, index, vals[0], vals[1], vals[2], vals[3]);
                    }
                }
            }            
        }

        #endregion Implementation of GLGpuProgram
  }

    /// <summary>
    ///     Creates a new ARB gpu program.
    /// </summary>
    public class ARBGpuProgramFactory : IOpenGLGpuProgramFactory {
        #region IOpenGLGpuProgramFactory Implementation

        public GLGpuProgram Create(string name, GpuProgramType type, string syntaxCode) {
            return new ARBGpuProgram(name, type, syntaxCode);
        }

        #endregion IOpenGLGpuProgramFactory Implementation
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.