@@ -51,5 +51,130 @@ public static void Finish()
5151 NativeMethods . HandleException (
5252 NativeMethods . core_ocl_finish ( ) ) ;
5353 }
54+
55+ /// <summary>
56+ /// Returns snapshots of the available OpenCL platforms and their devices.
57+ /// </summary>
58+ /// <remarks>
59+ /// Returns an empty list when no OpenCL runtime is available. The returned objects do not own native OpenCL handles.
60+ /// </remarks>
61+ public static IReadOnlyList < OclPlatformInfo > GetPlatformsInfo ( )
62+ {
63+ if ( ! HaveOpenCL ( ) )
64+ return Array . Empty < OclPlatformInfo > ( ) ;
65+
66+ NativeMethods . HandleException (
67+ NativeMethods . core_ocl_getPlatformsInfo ( out var nativePointer ) ) ;
68+ using var nativePlatforms = new OclPlatformInfoVector ( nativePointer ) ;
69+
70+ NativeMethods . HandleException (
71+ NativeMethods . core_ocl_PlatformInfoVector_size (
72+ nativePlatforms . Handle ,
73+ out var platformCount ) ) ;
74+
75+ var platforms = new OclPlatformInfo [ platformCount ] ;
76+ for ( var platformIndex = 0 ; platformIndex < platformCount ; platformIndex ++ )
77+ {
78+ platforms [ platformIndex ] = ReadPlatform ( nativePlatforms , platformIndex ) ;
79+ }
80+ return Array . AsReadOnly ( platforms ) ;
81+ }
82+
83+ private static OclPlatformInfo ReadPlatform ( OclPlatformInfoVector nativePlatforms , int platformIndex )
84+ {
85+ using var name = new StdString ( ) ;
86+ using var vendor = new StdString ( ) ;
87+ using var version = new StdString ( ) ;
88+ NativeMethods . HandleException (
89+ NativeMethods . core_ocl_PlatformInfoVector_getPlatform (
90+ nativePlatforms . Handle ,
91+ platformIndex ,
92+ name . CvPtr ,
93+ vendor . CvPtr ,
94+ version . CvPtr ,
95+ out var versionMajor ,
96+ out var versionMinor ,
97+ out var deviceCount ) ) ;
98+
99+ var devices = new OclDeviceInfo [ deviceCount ] ;
100+ for ( var deviceIndex = 0 ; deviceIndex < deviceCount ; deviceIndex ++ )
101+ {
102+ devices [ deviceIndex ] = ReadDevice ( nativePlatforms , platformIndex , deviceIndex ) ;
103+ }
104+
105+ return new OclPlatformInfo (
106+ name . ToString ( ) ,
107+ vendor . ToString ( ) ,
108+ version . ToString ( ) ,
109+ versionMajor ,
110+ versionMinor ,
111+ Array . AsReadOnly ( devices ) ) ;
112+ }
113+
114+ private static OclDeviceInfo ReadDevice (
115+ OclPlatformInfoVector nativePlatforms ,
116+ int platformIndex ,
117+ int deviceIndex )
118+ {
119+ using var name = new StdString ( ) ;
120+ using var vendorName = new StdString ( ) ;
121+ using var version = new StdString ( ) ;
122+ using var openCLVersion = new StdString ( ) ;
123+ using var openCLCVersion = new StdString ( ) ;
124+ using var driverVersion = new StdString ( ) ;
125+ NativeMethods . HandleException (
126+ NativeMethods . core_ocl_PlatformInfoVector_getDevice (
127+ nativePlatforms . Handle ,
128+ platformIndex ,
129+ deviceIndex ,
130+ name . CvPtr ,
131+ vendorName . CvPtr ,
132+ version . CvPtr ,
133+ openCLVersion . CvPtr ,
134+ openCLCVersion . CvPtr ,
135+ driverVersion . CvPtr ,
136+ out var type ,
137+ out var addressBits ,
138+ out var available ,
139+ out var compilerAvailable ,
140+ out var linkerAvailable ,
141+ out var maxClockFrequency ,
142+ out var maxComputeUnits ,
143+ out var globalMemorySize ,
144+ out var localMemorySize ,
145+ out var hostUnifiedMemory ,
146+ out var imageSupport ) ) ;
147+
148+ return new OclDeviceInfo (
149+ name . ToString ( ) ,
150+ vendorName . ToString ( ) ,
151+ version . ToString ( ) ,
152+ openCLVersion . ToString ( ) ,
153+ openCLCVersion . ToString ( ) ,
154+ driverVersion . ToString ( ) ,
155+ ( OclDeviceTypes ) type ,
156+ addressBits ,
157+ available != 0 ,
158+ compilerAvailable != 0 ,
159+ linkerAvailable != 0 ,
160+ maxClockFrequency ,
161+ maxComputeUnits ,
162+ globalMemorySize ,
163+ localMemorySize ,
164+ hostUnifiedMemory != 0 ,
165+ imageSupport != 0 ) ;
166+ }
167+
168+ private sealed class OclPlatformInfoVector : CvObject
169+ {
170+ public OclPlatformInfoVector ( IntPtr ptr )
171+ {
172+ SetSafeHandle ( new OpenCvPtrSafeHandle (
173+ ptr ,
174+ ownsHandle : true ,
175+ releaseAction : static p => NativeMethods . HandleException (
176+ NativeMethods . core_ocl_PlatformInfoVector_delete ( p ) ) ) ) ;
177+ }
178+ }
54179 }
55180}
0 commit comments