   glfwMakeContextCurrent(win);
   gladLoadGL(glfwGetProcAddress);
 
-  int deviceCount;
-  cudaGetDeviceCount(&deviceCount);
+  unsigned int deviceCount;
+  CHECKED_CUDA(cudaGLGetDevices(&deviceCount, nullptr, 0, cudaGLDeviceListAll));
   if (deviceCount == 0) {
     fprintf(stderr, "No CUDA-enabled devices available.");
     return EXIT_FAILURE;
   }
-  for (int i = 0; i < deviceCount; i++) {
+  std::vector<int> devices(deviceCount);
+  CHECKED_CUDA(cudaGLGetDevices(&deviceCount, devices.data(), deviceCount, cudaGLDeviceListAll));
+
+  bool found = false;
+  for (unsigned k = 0; k < deviceCount; k++) {
+    int i = devices[k];
     cudaDeviceProp dev_prop;
     cudaGetDeviceProperties(&dev_prop, i);
     fprintf(stderr, "%c[%i] %s cap=%d.%d\n", i == deviceIndex ? '*' : ' ', i, dev_prop.name, dev_prop.major, dev_prop.minor);
-  }
-  if (deviceIndex < 0 || deviceCount <= deviceIndex) {
+    if (i == deviceIndex) {
+      found = true;
+    }
+  }
+  if (!found) {
     fprintf(stderr, "Illegal CUDA device index %d\n", deviceIndex);
     return EXIT_FAILURE;
   }

#include <cuda_runtime_api.h>
#include <glad/gl.h>
#include <GLFW/glfw3.h>

#include <cmath>
#include <cassert>

    float frustum_shift_rz_ry_rx[16];
    matrixMul4(frustum_shift_rz_ry_rx, frustum, shift_rz_ry_rx);

#if 0
    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glUseProgram(simplePrg);
