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)
166#ifdef WITH_DAWN_NATIVE
172void emscripten_sleep(
unsigned int ms);
187#ifdef WITH_DAWN_NATIVE
192 results.instance =
nullptr;
196 auto *results = (Results *)userdata;
199 results->success =
false;
202 results->adapter = adapter;
211#ifdef WITH_DAWN_NATIVE
215 results->success =
false;
219 requestedLimits.limits.maxBufferSize = supportedLimits.limits.maxBufferSize;
220 requestedLimits.limits.maxStorageBufferBindingSize = supportedLimits.limits.maxStorageBufferBindingSize;
221 requestedLimits.limits.maxComputeWorkgroupStorageSize = supportedLimits.limits.maxComputeWorkgroupStorageSize;
227 desc.label =
nullptr;
228 desc.requiredFeaturesCount = 0;
229 desc.requiredFeatures =
nullptr;
230 desc.requiredLimits = &requestedLimits;
236 auto *results = (Results *)userdata;
238 results->success =
false;
241 results->device = device;
246 fprintf(stderr,
"WGPU Device Lost: %d %s", (
int)reason, message);
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;
297#ifdef WITH_DAWN_NATIVE
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
WGPU_EXPORT void wgpuDeviceRelease(WGPUDevice device)
@ WGPUBufferUsage_MapRead
@ WGPUBufferUsage_CopyDst
@ WGPURequestDeviceStatus_Success
struct WGPUInstanceImpl * WGPUInstance
WGPU_EXPORT void wgpuAdapterRequestDevice(WGPUAdapter adapter, WGPUDeviceDescriptor const *descriptor, WGPURequestDeviceCallback callback, void *userdata)
WGPU_EXPORT void wgpuAdapterRelease(WGPUAdapter adapter)
struct WGPUDeviceImpl * WGPUDevice
WGPU_EXPORT void wgpuInstanceRelease(WGPUInstance instance)
WGPU_EXPORT void wgpuDeviceSetDeviceLostCallback(WGPUDevice device, WGPUDeviceLostCallback callback, void *userdata)
WGPU_EXPORT WGPUBuffer wgpuDeviceCreateBuffer(WGPUDevice device, WGPUBufferDescriptor const *descriptor)
WGPU_EXPORT bool wgpuAdapterGetLimits(WGPUAdapter adapter, WGPUSupportedLimits *limits)
WGPU_EXPORT WGPUInstance wgpuCreateInstance(WGPUInstanceDescriptor const *descriptor)
WGPU_EXPORT void wgpuInstanceRequestAdapter(WGPUInstance instance, WGPURequestAdapterOptions const *options, WGPURequestAdapterCallback callback, void *userdata)
WGPU_EXPORT void wgpuBufferRelease(WGPUBuffer buffer)
struct WGPUAdapterImpl * WGPUAdapter
struct WGPUBufferImpl * WGPUBuffer
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
WGPUChainedStruct const * nextInChain
WGPUChainedStructOut * nextInChain