/// <summary> /// Disable the generic vertex attribute. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> on which the shader program is bound. /// </param> /// <param name="attributeBinding"> /// The <see cref="ShaderProgram.AttributeBinding"/> representing the generic vertex attribute. /// </param> internal override void DisableVertexAttribute(GraphicsContext ctx, ShaderProgram.AttributeBinding attributeBinding) { // Base implementation base.DisableVertexAttribute(ctx, attributeBinding); // Attribute divisor (not instanced) Gl.VertexAttribDivisor(attributeBinding.Location, 0); }
/// <summary> /// Enable the generic vertex attribute. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> on which the shader program is bound. /// </param> /// <param name="attributeBinding"> /// The <see cref="ShaderProgram.AttributeBinding"/> representing the generic vertex attribute. /// </param> internal override void EnableVertexAttribute(GraphicsContext ctx, ShaderProgram.AttributeBinding attributeBinding) { // Base implementation base.EnableVertexAttribute(ctx, attributeBinding); // Attribute divisor Gl.VertexAttribDivisor(attributeBinding.Location, Divisor); }
/// <summary> /// Set the vertex attribute. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> used for rendering. /// </param> /// <param name="shaderProgram"> /// The <see cref="ShaderProgram"/> that is used for rendering. /// </param> /// <param name="attributeName"> /// A <see cref="String"/> that specify the attribute which binds to this vertex array. /// </param> public void SetVertexAttribute(GraphicsContext ctx, ShaderProgram shaderProgram, string attributeName) { if (attributeName == null) { throw new ArgumentNullException("attributeName"); } ShaderProgram.AttributeBinding attributeBinding = shaderProgram.GetActiveAttribute(attributeName); switch (attributeBinding.Type) { case ShaderAttributeType.Float: Gl.VertexAttrib1(attributeBinding.Location, (float)DefaultValue.x); break; case ShaderAttributeType.Vec2: Gl.VertexAttrib2(attributeBinding.Location, (float)DefaultValue.x, (float)DefaultValue.y); break; case ShaderAttributeType.Vec3: Gl.VertexAttrib3(attributeBinding.Location, (float)DefaultValue.x, (float)DefaultValue.y, (float)DefaultValue.z); break; case ShaderAttributeType.Vec4: Gl.VertexAttrib4(attributeBinding.Location, (float)DefaultValue.x, (float)DefaultValue.y, (float)DefaultValue.z, (float)DefaultValue.w); break; default: throw new NotImplementedException(String.Format("default value for attributes of type {0} not implemented", attributeBinding.Type)); } }
/// <summary> /// Set the vertex attribute. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> used for rendering. /// </param> /// <param name="shaderProgram"> /// The <see cref="ShaderProgram"/> that is used for rendering. /// </param> /// <param name="attributeName"> /// A <see cref="String"/> that specify the attribute which binds to this vertex array. /// </param> public void SetVertexAttribute(GraphicsContext ctx, ShaderProgram shaderProgram, string attributeName) { if (attributeName == null) { throw new ArgumentNullException("attributeName"); } if (shaderProgram != null) { ShaderProgram.AttributeBinding attributeBinding = shaderProgram.GetActiveAttribute(attributeName); // Avoid rendundant buffer binding and relative vertex array setup if (ctx.Caps.GlExtensions.VertexArrayObject_ARB && IsDirty == false) { CheckVertexAttribute(ctx, attributeBinding); return; } // Enable/Disable shader attribute if (ArrayBuffer != null) { EnableVertexAttribute(ctx, attributeBinding); } else { DisableVertexAttribute(ctx, attributeBinding); } } else { switch (attributeName) { case VertexArraySemantic.Position: SetPositionAttribute(ctx); break; case VertexArraySemantic.Color: SetColorAttribute(ctx); break; case VertexArraySemantic.Normal: SetNormalAttribute(ctx); break; case VertexArraySemantic.TexCoord: SetTexCoordAttribute(ctx); break; default: throw new NotSupportedException(String.Format("attribute {0} not supported on fixed pipeline", attributeName)); } } // Next time do not set bindings and array state if GL_ARB_vertex_array_object is supported IsDirty = false; }
private void EnableVertexAttribute(GraphicsContext ctx, ShaderProgram.AttributeBinding attributeBinding) { ArrayBufferObjectBase.IArraySection arraySection = ArrayBuffer.GetArraySection(ArraySectionIndex); int arrayBaseType = (int)ArrayBufferItem.GetArrayBaseType(arraySection.ItemType); int arrayLength = (int)ArrayBufferItem.GetArrayLength(arraySection.ItemType); int arrayStride = arraySection.Stride.ToInt32(); // Avoid rendundant buffer binding and relative vertex array setup if (ctx.Caps.GlExtensions.VertexArrayObject_ARB && IsDirty == false) { CheckVertexAttribute(ctx, attributeBinding); return; } // Bind the array buffer ArrayBuffer.Bind(ctx); // Bind varying attribute to currently bound buffer object switch (ArrayBufferItem.GetArrayBaseType(attributeBinding.Type)) { case VertexBaseType.Float: Gl.VertexAttribPointer( attributeBinding.Location, arrayLength, arrayBaseType, arraySection.Normalized, arrayStride, arraySection.Offset ); break; case VertexBaseType.Int: case VertexBaseType.UInt: Gl.VertexAttribIPointer( attributeBinding.Location, arrayLength, arrayBaseType, arrayStride, arraySection.Offset ); break; case VertexBaseType.Double: Gl.VertexAttribLPointer( attributeBinding.Location, arrayLength, arrayBaseType, arrayStride, arraySection.Offset ); break; default: throw new NotSupportedException(String.Format("vertex attribute type {0} not supported", attributeBinding.Type)); } // Enable vertex attribute Gl.EnableVertexAttribArray(attributeBinding.Location); }
internal void CheckVertexAttribute(GraphicsContext ctx, ShaderProgram.AttributeBinding attributeBinding) { if (ArrayBuffer != null) { ArrayBufferObjectBase.IArraySection arraySection = ArrayBuffer.GetArraySection(ArraySectionIndex); int arrayBaseType = (int)ArrayBufferItem.GetArrayBaseType(arraySection.ItemType); int arrayLength = (int)ArrayBufferItem.GetArrayLength(arraySection.ItemType); int arrayStride = arraySection.Stride.ToInt32(); // Check effective state IntPtr vertexAttribPointer; int vertexAttribArrayBufferBinding; int vertexAttribArraySize; int vertexAttribArrayType; int vertexAttribArrayNormalized; int vertexAttribArrayStride; int vertexAttribArrayEnabled; // Attribute pointer/offset Gl.GetVertexAttribPointer(attributeBinding.Location, Gl.VERTEX_ATTRIB_ARRAY_POINTER, out vertexAttribPointer); Debug.Assert(vertexAttribPointer == new IntPtr(arraySection.Pointer.ToInt64() + arraySection.Offset.ToInt64())); // Array buffer binding Gl.GetVertexAttrib(attributeBinding.Location, Gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, out vertexAttribArrayBufferBinding); Debug.Assert(vertexAttribArrayBufferBinding == ArrayBuffer.ObjectName); // Array size Gl.GetVertexAttrib(attributeBinding.Location, Gl.VERTEX_ATTRIB_ARRAY_SIZE, out vertexAttribArraySize); Debug.Assert(vertexAttribArraySize == arrayLength); // Array type Gl.GetVertexAttrib(attributeBinding.Location, Gl.VERTEX_ATTRIB_ARRAY_TYPE, out vertexAttribArrayType); Debug.Assert(vertexAttribArrayType == arrayBaseType); // Array normalized Gl.GetVertexAttrib(attributeBinding.Location, Gl.VERTEX_ATTRIB_ARRAY_NORMALIZED, out vertexAttribArrayNormalized); Debug.Assert((vertexAttribArrayNormalized != 0) == arraySection.Normalized); // Array size Gl.GetVertexAttrib(attributeBinding.Location, Gl.VERTEX_ATTRIB_ARRAY_STRIDE, out vertexAttribArrayStride); Debug.Assert(vertexAttribArrayStride == arrayStride); // Attribute enabled Gl.GetVertexAttrib(attributeBinding.Location, Gl.VERTEX_ATTRIB_ARRAY_ENABLED, out vertexAttribArrayEnabled); Debug.Assert(vertexAttribArrayEnabled == Gl.TRUE); } else { int vertexAttribArrayEnabled; // Attribute disabled Gl.GetVertexAttrib(attributeBinding.Location, Gl.VERTEX_ATTRIB_ARRAY_ENABLED, out vertexAttribArrayEnabled); Debug.Assert(vertexAttribArrayEnabled == Gl.FALSE); } }
/// <summary> /// Utility routine for checking the vertex array state. /// </summary> /// <param name="ctx"> /// A <see cref="GraphicsContext"/> on which the shader program is bound. /// </param> /// <param name="shaderProgram"> /// A <see cref="ShaderProgram"/> on which the vertex arrays shall be bound. /// </param> private void CheckVertexAttributes(GraphicsContext ctx, ShaderProgram shaderProgram) { // Vertex array state overall check foreach (string attributeName in shaderProgram.ActiveAttributes) { ShaderProgram.AttributeBinding attributeBinding = shaderProgram.GetActiveAttribute(attributeName); VertexArray shaderVertexArray = GetVertexArray(attributeName, shaderProgram); if (shaderVertexArray == null) { continue; } shaderVertexArray.CheckVertexAttribute(ctx, attributeBinding); } }
private void DisableVertexAttribute(GraphicsContext ctx, ShaderProgram.AttributeBinding attributeBinding) { // Avoid rendundant buffer binding and relative vertex array setup if (ctx.Caps.GlExtensions.VertexArrayObject_ARB && IsDirty == false) { #if DEBUG // Check effective state int vertexAttribArrayEnabled; // Attribute enabled Gl.GetVertexAttrib(attributeBinding.Location, Gl.VERTEX_ATTRIB_ARRAY_ENABLED, out vertexAttribArrayEnabled); Debug.Assert(vertexAttribArrayEnabled == Gl.TRUE); #endif return; } // Enable vertex attribute Gl.DisableVertexAttribArray(attributeBinding.Location); }
/// <summary> /// Disable the generic vertex attribute. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> on which the shader program is bound. /// </param> /// <param name="attributeBinding"> /// The <see cref="ShaderProgram.AttributeBinding"/> representing the generic vertex attribute. /// </param> internal virtual void DisableVertexAttribute(GraphicsContext ctx, ShaderProgram.AttributeBinding attributeBinding) { // Enable vertex attribute Gl.DisableVertexAttribArray(attributeBinding.Location); }