我在使用Windows和Linux平臺的渲染系統時遇到了一些麻煩。glXMakeCurrent給我不好匹配
在Windows平臺上它就像一個沙姆沙伊赫,而在Linux上我的應用程序有以下異常終止:
System.InvalidOperationException: context cannot be current ---> System.ComponentModel.Win32Exception: BadMatch (invalid parameter attributes) X error details: X event name: '' (0) Display: 0x8f17328 Resource ID: 4000011 Error code: 8 Major code: '' (135) Minor code: 5
at Derm.Render.RenderContext.MakeCurrent (IDeviceContext deviceContext, Boolean flag) at Derm.Render.RenderContext.CreateRenderContext (IDeviceContext deviceContext, Derm.Render.RenderContext hSharedContext, GLVersion version) Stacktrace:
at (wrapper managed-to-native) System.Windows.Forms.XplatUIX11.XFlush (intptr) at System.Windows.Forms.XplatUIX11.PostQuitMessage (int)
...
at System.Windows.Forms.Control.CreateHandle() at System.Windows.Forms.Control.CreateControl()
我能創造一個「簡單」的渲染上下文,使用下列程序:
private static IntPtr CreateX11SimpleContext(IDeviceContext rDevice)
{
XServerDeviceContext x11DeviceCtx = (XServerDeviceContext)rDevice;
using (new Glx.XLock(x11DeviceCtx.Display)) {
int[] attributes = new int[] {
Glx.RENDER_TYPE, (int)Glx.RGBA_BIT,
0
};
// Get basic visual
unsafe {
int[] choosenConfigCount = new int[1];
IntPtr *choosenConfigs = Glx.ChooseFBConfig(x11DeviceCtx.Display, x11DeviceCtx.Screen, attributes, ref choosenConfigCount);
if (choosenConfigCount[0] == 0)
throw new InvalidOperationException("unable to find basic visual");
IntPtr choosenConfig = *choosenConfigs;
IntPtr visual = Glx.GetVisualFromFBConfig(x11DeviceCtx.Display, choosenConfig);
x11DeviceCtx.XVisualInfo = (Glx.XVisualInfo)Marshal.PtrToStructure(visual, typeof(Glx.XVisualInfo));
x11DeviceCtx.FBConfig = choosenConfig;
Glx.XFree((IntPtr)choosenConfigs);
}
// Create direct context
IntPtr rContext = Glx.CreateContext(x11DeviceCtx.Display, x11DeviceCtx.XVisualInfo, IntPtr.Zero, true);
if (rContext == IntPtr.Zero) {
// Fallback to not direct context
rContext = Glx.CreateContext(x11DeviceCtx.Display, x11DeviceCtx.XVisualInfo, IntPtr.Zero, false);
}
if (rContext == IntPtr.Zero)
throw new InvalidOperationException("unable to create context");
return (rContext);
}
}
我面臨的問題是上述上下文用於獲取OpenGL信息(擴展名,渲染器,...),然後被銷燬。接着,我創建使用屬性上下文:
XServerDeviceContext x11DeviceContext = (XServerDeviceContext)deviceContext;
using (Glx.XLock displayLock = new Glx.XLock(x11DeviceContext.Display)) {
return (Glx.CreateContextAttribsARB(x11DeviceContext.Display, x11DeviceContext.FBConfig, sharedContext, true, attribsList));
}
的情況下被創建,但在接下來的glXMakeCurrent
X服務器發送我的錯誤問題(BadMatch)。
我懷疑CreateContextAttribsARB
參數:x11DeviceContext.Display
和x11DeviceContext.FBConfig
。事實上,我使用的「drawable」實際上是一個System.Windows.Forms
控件,由Mono實現提供。
這裏是展示我如何初始化這些變量的一些片段:
如何初始化x11DeviceContext.Display
?
Type xplatui = Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms");
Type xplatui = Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms");
if (xplatui == null)
throw new PlatformNotSupportedException("mono runtime version no supported");
// Get System.Windows.Forms display
mDisplay = (IntPtr)xplatui.GetField("DisplayHandle", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).GetValue(null);
if (mDisplay == IntPtr.Zero)
throw new InvalidOperationException("unable to connect to X server");
我該如何初始化x11DeviceContext.FBConfig
?
IntPtr* configs = Glx.GetFBConfigs(x11DeviceContext.Display, x11DeviceContext.Screen, out configsCount);
// Then, a value is selected using a custom seletcion algo, using GetFBConfigAttrib
對不起,因爲我不能給你一個簡單的例子程序,但該代碼庫是非常龐大和複雜。
你知道它發生了什麼嗎?
編輯:
進一步調查顯示,我可以正確顯示使用特定的視覺效果,否則我得到BadMatch
。我不能說爲什麼,但我看到我的線和三角形(但即使視覺不是雙緩衝,我也需要交換)。
以前我得到了BadMatch
錯誤,因爲我沒有意識到「不符合」的視覺效果,實際上我選擇了其中的一種;然而,大多數的視覺給我的錯誤。
我檢查了單聲道實現的XplatUIX11
類,實際上用XOpenDisplay
打開了顯示。
上下文在其創建之後變爲當前,其中發生錯誤。 C#綁定是由我定義的,我完全可以控制。你對XLock課是對的。在tomorro之前,我可以上傳整個框架(由GPL編寫並由我編寫) – Luca
@Luca:爲了學習的目的,您是否也編寫了一個純C程序來創建GLX上下文?只是看到一般的方法有效嗎?我在https://github.com/datenwolf/codesamples/tree/master/samples/OpenGL/x11argb_opengl – datenwolf
上提供了一個基於X11 OpenGL FBConfig的上下文創建的非常簡單的例子。它的工作原理類似於sharm(很好的技巧)。 – Luca