Esempio n. 1
0
		public BitmapFont (Stream stream, Shader shader = null) {
			ThreadContext.Current.EnsureGLContext();

			if (shader == null)
				shader = new SpriteShader();

			var tr = new TarReader(stream);
			while (tr.MoveNext(false)) {
				switch (tr.FileInfo.FileName) {
					case "font.atlas":
						using (var atlasStream = new MemoryStream()) {
							tr.Read(atlasStream);
							atlasStream.Position = 0;
							this.ParseStream(atlasStream);
						}
						break;
					case "font.png":
						using (var textureStream = new MemoryStream()) {
							tr.Read(textureStream);
							textureStream.Position = 0;

							_material = new SpriteMaterial(shader, new Texture (textureStream));
						}
						break;
					default:
						throw new ContentException("Unrecognized font file " + tr.FileInfo.FileName);
				}
			}

			if (_chars == null)
				throw new ContentException("Missing font atlas.");
			if (_material == null)
				throw new ContentException("Missing font texture.");
		}
Esempio n. 2
0
		public Material (Shader shader) {
			_shader = shader;
		}
Esempio n. 3
0
		public BitmapFont (string path, Shader shader = null) : this(Assets.ResolveStream(path), shader) {
		}
Esempio n. 4
0
		public Shader (string vertSource, string fragSource, string name = null) {
			_name = name ?? this.GetType().FullName;
			var glContext = ThreadContext.Current.EnsureGLContext();
			lock (_shaders) {
				Dictionary<string, Shader> shaders;
				if (_shaders.TryGetValue(glContext, out shaders)) {
					Shader master;
					if (shaders.TryGetValue(_name, out master)) {
						_master = master;
						_master._refCount++;
						_handle = master._handle;
						_vertHandle = master._vertHandle;
						_fragHandle = master._fragHandle;
						_uniforms = master._uniforms;
						_attributes = master._attributes;
						return;
					}
				} else {
					_shaders.Add(glContext, new Dictionary<string, Shader>());
				}
				_shaders[glContext].Add(_name, this);
			}
			_master = this;

			// create shader program and link
#if __ANDROID__
            _vertHandle = (uint)GL.CreateShader(All.VertexShader);
            _fragHandle = (uint)GL.CreateShader(All.FragmentShader);
#else
			_vertHandle = (uint)GL.CreateShader(ShaderType.VertexShader);
			_fragHandle = (uint)GL.CreateShader(ShaderType.FragmentShader);
#endif

			if (_vertHandle <= 0 || _fragHandle <= 0)
				throw new ShaderException("Failed to create shader.");
			_handle = (uint)GL.CreateProgram();
			if (_handle <= 0)
				throw new ShaderException("Failed to create program.");
			CompileShader(_vertHandle, vertSource);
			CompileShader(_fragHandle, fragSource);
			GL.AttachShader(_handle, _vertHandle);
			GL.AttachShader(_handle, _fragHandle);
			GL.LinkProgram(_handle);

			// catalog linked shader uniforms
			int total = 0;
#if __ANDROID__
            GL.GetProgram(_handle, All.ActiveUniforms, out total);
#else
			GL.GetProgram(_handle, ProgramParameter.ActiveUniforms, out total);
#endif
			var sb = new StringBuilder(100);
			_uniforms = new Dictionary<string, int>();
			for (var i = 0; i < total; ++i) {
				int length = 0, size = 0;
				#if __ANDROID__
                All type = All.None;
                GL.GetActiveUniform((uint)_handle, (uint)i, 100, out length, out size, out type, name);
				#else
				ActiveUniformType type;
				GL.GetActiveUniform(_handle, (uint)i, 100, out length, out size, out type, sb);
				#endif
				var n = sb.ToString();
				#if __ANDROID__
                _uniforms.Add(n, GL.GetUniformLocation(_handle, name));
				#else
				_uniforms.Add(n, GL.GetUniformLocation(_handle, n));
				#endif
				sb.Length = 0;
			}

			// catalog linked vertex attributes
#if __ANDROID__
            GL.GetProgram(_handle, All.ActiveAttributes, out total);
#else
			GL.GetProgram(_handle, ProgramParameter.ActiveAttributes, out total);
#endif
			_attributes = new Dictionary<string, int>();
			for (var i = 0; i < total; ++i) {
				int length = 0, size = 0;
				#if __ANDROID__
                All type;
				#else
				ActiveAttribType type;
				#endif
				GL.GetActiveAttrib(_handle, (uint)i, 100, out length, out size, out type, sb);
				var n = sb.ToString();
				#if __ANDROID__
                _attributes.Add(n, GL.GetAttribLocation(_handle, name));
				#else
				_attributes.Add(n, GL.GetAttribLocation(_handle, n));
				#endif
				sb.Length = 0;
			}

			_refCount = 1;
			Interlocked.Increment(ref _shaderCount);
		}
Esempio n. 5
0
		public MeshMaterial (Shader shader = null, string name = "") : base(shader) {
			_name = name;
			_polygonState = new int[2];
		}
Esempio n. 6
0
		static void SetTextures(Shader shader, ref int unit, TextureSlot[] stack, string[] names, string[] blendNames) {
			for (var i = 0; i < stack.Length; i++) {
				var slot = stack[i];
				GL.ActiveTexture(TextureUnit.Texture0 + unit);
				GL.BindTexture(TextureTarget.Texture2D, slot.Texture.Handle);
				GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)slot.WrapS);
				GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)slot.WrapT);
				shader.Uniform(names[i], unit++);
				shader.Uniform(blendNames[i], slot.BlendFactor);
			}
		}
Esempio n. 7
0
        public Shader(string vertSource, string fragSource, string name = null)
        {
            _name = name ?? this.GetType().FullName;
            var glContext = ThreadContext.Current.EnsureGLContext();

            lock (_shaders) {
                Dictionary <string, Shader> shaders;
                if (_shaders.TryGetValue(glContext, out shaders))
                {
                    Shader master;
                    if (shaders.TryGetValue(_name, out master))
                    {
                        _master = master;
                        _master._refCount++;
                        _handle     = master._handle;
                        _vertHandle = master._vertHandle;
                        _fragHandle = master._fragHandle;
                        _uniforms   = master._uniforms;
                        _attributes = master._attributes;
                        return;
                    }
                }
                else
                {
                    _shaders.Add(glContext, new Dictionary <string, Shader>());
                }
                _shaders[glContext].Add(_name, this);
                _master = this;

                // create shader program and link
#if __ANDROID__
                _vertHandle = (uint)GL.CreateShader(All.VertexShader);
                _fragHandle = (uint)GL.CreateShader(All.FragmentShader);
#else
                _vertHandle = (uint)GL.CreateShader(ShaderType.VertexShader);
                _fragHandle = (uint)GL.CreateShader(ShaderType.FragmentShader);
#endif

                if (_vertHandle <= 0 || _fragHandle <= 0)
                {
                    throw new ShaderException("Failed to create shader.");
                }
                _handle = (uint)GL.CreateProgram();
                if (_handle <= 0)
                {
                    throw new ShaderException("Failed to create program.");
                }
                CompileShader(_vertHandle, vertSource);
                CompileShader(_fragHandle, fragSource);
                GL.AttachShader(_handle, _vertHandle);
                GL.AttachShader(_handle, _fragHandle);
                GL.LinkProgram(_handle);
            }

            // catalog linked shader uniforms
            int total = 0;
#if __ANDROID__
            GL.GetProgram(_handle, All.ActiveUniforms, out total);
#else
            GL.GetProgram(_handle, ProgramParameter.ActiveUniforms, out total);
#endif
            var sb = new StringBuilder(100);
            _uniforms = new Dictionary <string, int>();
            for (var i = 0; i < total; ++i)
            {
                int length = 0, size = 0;
                ActiveUniformType type;
                                #if __ANDROID__
                GL.GetActiveUniform((uint)_handle, (uint)i, 100, out length, out size, out type, sb);
                                #else
                GL.GetActiveUniform(_handle, (uint)i, 100, out length, out size, out type, sb);
                                #endif
                var n = sb.ToString();
                                #if __ANDROID__
                _uniforms.Add(n, GL.GetUniformLocation(_handle, sb));
                                #else
                _uniforms.Add(n, GL.GetUniformLocation(_handle, n));
                                #endif
                sb.Length = 0;
            }

            // catalog linked vertex attributes
#if __ANDROID__
            GL.GetProgram(_handle, All.ActiveAttributes, out total);
#else
            GL.GetProgram(_handle, ProgramParameter.ActiveAttributes, out total);
#endif
            _attributes = new Dictionary <string, int>();
            for (var i = 0; i < total; ++i)
            {
                int length = 0, size = 0;
                                #if __ANDROID__
                All type;
                                #else
                ActiveAttribType type;
                                #endif
                GL.GetActiveAttrib(_handle, (uint)i, 100, out length, out size, out type, sb);
                var n = sb.ToString();
                                #if __ANDROID__
                _attributes.Add(n, GL.GetAttribLocation(_handle, sb));
                                #else
                _attributes.Add(n, GL.GetAttribLocation(_handle, n));
                                #endif
                sb.Length = 0;
            }

            _refCount = 1;
        }
Esempio n. 8
0
		public SpriteMaterial (Shader shader, Texture texture) : base(shader) {
			_texture = texture;
			this.Color = Vector4.One;
		}