1#if defined(TEST_OPENCL)
4#define CL_TARGET_OPENCL_VERSION 120
5#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
20 err = clGetPlatformIDs(maxPlatforms, platforms, &platformCount);
22 printf(
"clGetPlatformIDs failed (%d)\n", err);
28 if (platformCount > 0) {
29 platform = platforms[0];
31 if (platform ==
nullptr) {
32 printf(
"Failed to get platform\n");
42 err = clGetDeviceIDs(platform, device_type, maxDevices, devices, &deviceCount);
44 printf(
"clGetDeviceIDs failed (%d)\n", err);
47 if (deviceCount == 0) {
48 printf(
"Failed to get device\n");
57 cl_ctx = clCreateContext(properties, 1, &dev,
nullptr,
nullptr, &err);
59 printf(
"clCreateContext failed (%d)\n", err);
63 cl_q = clCreateCommandQueue(cl_ctx, dev, 0, &err);
65 printf(
"clCreateCommandQueue failed (%d)\n", err);
72 clReleaseCommandQueue(cl_q);
73 clReleaseContext(cl_ctx);
76#elif defined(TEST_CUDA)
80inline bool create_cuda_context(CUcontext &cuda_ctx) {
83 if (err != CUDA_SUCCESS) {
84 printf(
"cuInit failed (%d)\n", err);
90 err = cuDeviceGetCount(&deviceCount);
91 if (err != CUDA_SUCCESS) {
92 printf(
"cuGetDeviceCount failed (%d)\n", err);
95 if (deviceCount <= 0) {
96 printf(
"No CUDA devices available\n");
105 if (deviceCount > 2) deviceCount = 2;
106 for (
int id = deviceCount - 1;
id >= 0;
id--) {
107 status = cuDeviceGet(&dev,
id);
108 if (status == CUDA_SUCCESS)
break;
111 if (status != CUDA_SUCCESS) {
112 printf(
"Failed to get CUDA device\n");
117 err = cuCtxCreate(&cuda_ctx, 0, dev);
118 if (err != CUDA_SUCCESS) {
119 printf(
"cuCtxCreate failed (%d)\n", err);
126inline void destroy_cuda_context(CUcontext cuda_ctx) {
127 cuCtxDestroy(cuda_ctx);
130#elif defined(TEST_METAL) && defined(__OBJC__)
131#include <Metal/MTLCommandQueue.h>
132#include <Metal/MTLDevice.h>
134inline bool create_metal_context(id<MTLDevice> &device, id<MTLCommandQueue> &queue) {
135 device = MTLCreateSystemDefaultDevice();
136 if (device ==
nullptr) {
137 NSArray<id<MTLDevice>> *devices = MTLCopyAllDevices();
138 if (devices !=
nullptr) {
142 if (device ==
nullptr) {
143 printf(
"Failed to find Metal device.\n");
146 queue = [device newCommandQueue];
147 if (queue ==
nullptr) {
148 printf(
"Failed to create Metal command queue.\n");
154inline void destroy_metal_context(id<MTLDevice> device, id<MTLCommandQueue> queue) {
159#elif defined(TEST_WEBGPU)
161#if defined(__EMSCRIPTEN__)
162#include <webgpu/webgpu_cpp.h>
170#ifdef WITH_DAWN_NATIVE
176void emscripten_sleep(
unsigned int ms);
180inline bool create_webgpu_context(WGPUInstance *instance_out, WGPUAdapter *adapter_out, WGPUDevice *device_out, WGPUBuffer *staging_buffer_out) {
182 WGPUInstance instance =
nullptr;
183 WGPUAdapter adapter =
nullptr;
184 WGPUDevice device =
nullptr;
185 WGPUBuffer staging_buffer =
nullptr;
191 auto request_adapter_callback = [](
WGPURequestAdapterStatus status, WGPUAdapter adapter,
char const *message,
void *userdata) {
192 auto *results = (Results *)userdata;
195 results->success =
false;
198 results->adapter = adapter;
207#ifdef WITH_DAWN_NATIVE
211 results->success =
false;
215 requestedLimits.limits.maxBufferSize = supportedLimits.limits.maxBufferSize;
216 requestedLimits.limits.maxStorageBufferBindingSize = supportedLimits.limits.maxStorageBufferBindingSize;
217 requestedLimits.limits.maxComputeWorkgroupStorageSize = supportedLimits.limits.maxComputeWorkgroupStorageSize;
228 fprintf(stderr,
"WGPU Device Lost: %d %s", (
int)reason, message);
234 desc.label =
nullptr;
235 desc.requiredFeatureCount = 0;
236 desc.requiredFeatures =
nullptr;
237 desc.requiredLimits = &requestedLimits;
238 desc.deviceLostCallback = device_lost_callback;
244 auto *results = (Results *)userdata;
246 results->success =
false;
249 results->device = device;
252 constexpr int kStagingBufferSize = 4 * 1024 * 1024;
255 desc.label =
nullptr;
257 desc.size = kStagingBufferSize;
258 desc.mappedAtCreation =
false;
260 if (results->staging_buffer ==
nullptr) {
261 results->success =
false;
272 while (!results.device && results.success) {
275#ifndef WITH_DAWN_NATIVE
276 emscripten_sleep(10);
282 *instance_out = results.instance;
283 *adapter_out = results.adapter;
284 *device_out = results.device;
285 *staging_buffer_out = results.staging_buffer;
286 return results.success;
289inline void destroy_webgpu_context(WGPUInstance instance, WGPUAdapter adapter, WGPUDevice device, WGPUBuffer staging_buffer) {
struct _cl_platform_id * cl_platform_id
intptr_t cl_context_properties
cl_bitfield cl_device_type
#define CL_DEVICE_TYPE_ALL
#define CL_CONTEXT_PLATFORM
struct _cl_context * cl_context
struct _cl_device_id * cl_device_id
struct _cl_command_queue * cl_command_queue
@ WGPURequestAdapterStatus_Success
@ WGPUDeviceLostReason_Destroyed
@ WGPUBufferUsage_MapRead
@ WGPUBufferUsage_CopyDst
WGPU_EXPORT void wgpuDeviceRelease(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuInstanceRelease(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE
@ WGPURequestDeviceStatus_Success
WGPU_EXPORT WGPUInstance wgpuCreateInstance(WGPU_NULLABLE WGPUInstanceDescriptor const *descriptor) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuAdapterRequestDevice(WGPUAdapter adapter, WGPU_NULLABLE WGPUDeviceDescriptor const *descriptor, WGPURequestDeviceCallback callback, void *userdata) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT WGPUBuffer wgpuDeviceCreateBuffer(WGPUDevice device, WGPUBufferDescriptor const *descriptor) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuInstanceRequestAdapter(WGPUInstance instance, WGPU_NULLABLE WGPURequestAdapterOptions const *options, WGPURequestAdapterCallback callback, void *userdata) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuBufferRelease(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT WGPUBool wgpuAdapterGetLimits(WGPUAdapter adapter, WGPUSupportedLimits *limits) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuAdapterRelease(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE
WEAK char device_type[256]
void * memset(void *s, int val, size_t n)
unsigned __INT32_TYPE__ uint32_t
WGPUChainedStruct const * nextInChain
WGPUChainedStruct const * nextInChain
WGPUChainedStruct const * nextInChain
WGPUChainedStructOut * nextInChain