sharpgl 无法调用扩展函数 glShaderSource
本文关键字:函数 glShaderSource 扩展 调用 sharpgl | 更新日期: 2023-09-27 18:36:12
尝试使用 sharpgl 创建着色器时,我收到以下异常
System.Exception: Cannot invoke extension function glShaderSource ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Cannot marshal: Encountered unmappable character.
at System.StubHelpers.MngdNativeArrayMarshaler.ConvertContentsToNative(IntPtr pMarshalState, Object& pManagedHome, IntPtr pNativeHome)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at SharpGL.OpenGL.InvokeExtensionFunction[T](Object[] args) in c:'Repositories'GitHub'sharpgl'source'SharpGL'Core'SharpGL'OpenGLExtensions.cs:line 66
--- End of inner exception stack trace ---
at SharpGL.OpenGL.InvokeExtensionFunction[T](Object[] args) in c:'Repositories'GitHub'sharpgl'source'SharpGL'Core'SharpGL'OpenGLExtensions.cs:line 70
at SharpGL.OpenGL.ShaderSource(UInt32 shader, String source) in c:'Repositories'GitHub'sharpgl'source'SharpGL'Core'SharpGL'OpenGLExtensions.cs:line 1296
at SharpGL.Shaders.Shader.Create(OpenGL gl, UInt32 shaderType, String source) in c:'Repositories'GitHub'sharpgl'source'SharpGL'Core'SharpGL'Shaders'Shader.cs:line 20
at SharpGL.Shaders.ShaderProgram.Create(OpenGL gl, String vertexShaderSource, String fragmentShaderSource, Dictionary`2 attributeLocations) in c:'Repositories'GitHub'sharpgl'source'SharpGL'Core'SharpGL'Shaders'ShaderProgram.cs:line 26
我目前使用的代码是
try
{
// Create the shader program.
var vertexShaderSource = @"#version 150 core
in vec3 in_Position;
in vec3 in_Color;
out vec3 pass_Color;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
void main(){gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);pass_Color = in_Color;}";
var fragmentShaderSource = @"#version 150 core
in vec3 pass_Color;
out vec4 out_Color;
void main(void){ out_Color = vec4(pass_Color, 1.0); }";
shaderProgram = new ShaderProgram();
shaderProgram.Create(gl, vertexShaderSource, fragmentShaderSource, null); // <- exception
shaderProgram.BindAttributeLocation(gl, attributeIndexPosition, "in_Position");
shaderProgram.BindAttributeLocation(gl, attributeIndexColour, "in_Color");
shaderProgram.AssertValid(gl);
}
catch (Exception e)
{
System.Diagnostics.Debug.Write( e.ToString() );
}
例外情况在shaderProgram.Create
行上。删除或缩小着色器源字符串时,不会发生错误,但着色器将无法编译,并且我收到Failed to compile shader with ID 2.
错误。我还在源代码中找到了异常来自的位置,但不知道如何解决此问题或实际原因是什么。
我已经解决了这个问题。逐个角色地检查着色器,他们发现在in_Color
之后是一个奇怪的角色。使用带有ANSI编码的记事本++查看源显示了一些不应该属于他们的字符。
为了将来参考,请查看不同编码的源,并检查是否有任何不应存在的字符。