有人尝试过从 C# 手动调用 Direct3D 接口吗?我编写了以下短代码并result == S_OK
在调用D3D11CreateDevice()
.
我希望GetCreationFlags()
返回32
,这是我调用时的参数之一D3D11CreateDevice()
,但我得到了AccessViolationException
。我在这里想念什么?
先感谢您。
[Guid("DB6F6DDB-AC77-4E88-8253-819DF9BBF140")]
interface ID3D11Device
{
[return : MarshalAs(UnmanagedType.U4)]
uint GetCreationFlags();
}
[DllImport("d3d11.dll", CallingConvention = CallingConvention.StdCall)]
private unsafe static extern int D3D11CreateDevice(
void* arg0, int arg1, void* arg2, int arg3, void* arg4, int arg5, int arg6,
[Out, MarshalAs(UnmanagedType.Interface)] out object arg7,
void* arg8, void* arg9);
public static void CreateDevice()
{
object deviceOut;
int arg8;
IntPtr arg9;
unsafe
{
int result = D3D11CreateDevice(
(void*)IntPtr.Zero, 1, (void*)IntPtr.Zero, 32,
(void*)IntPtr.Zero, 0, 7,
out deviceOut,
&arg8,
&arg9);
}
ID3D11Device dev = (ID3D11Device)deviceOut;
uint i = dev.GetCreationFlags(); // System.AccessViolationException
}
供您参考,原型D3D11CreateDevice
和ID3D11Device::GetCreationFlags()
外观如下:
HRESULT D3D11CreateDevice(
_In_ IDXGIAdapter *pAdapter,
_In_ D3D_DRIVER_TYPE DriverType,
_In_ HMODULE Software,
_In_ UINT Flags,
_In_ const D3D_FEATURE_LEVEL *pFeatureLevels,
_In_ UINT FeatureLevels,
_In_ UINT SDKVersion,
_Out_ ID3D11Device **ppDevice,
_Out_ D3D_FEATURE_LEVEL *pFeatureLevel,
_Out_ ID3D11DeviceContext **ppImmediateContext
);
UINT GetCreationFlags();