   glfwDestroyWindow(win);
   glfwTerminate();
 
-  glDeleteShader(simpleVS);
-
   return EXIT_SUCCESS;
 }

    }
    glBindVertexArray(cudaVbo);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glUseProgram(simplePrg);
    glUniformMatrix4fv(0, 1, GL_FALSE, normal_matrix);
    glUniformMatrix4fv(1, 1, GL_FALSE, modelview_projection);
    glUniform4f(2, 0.6f, 0.6f, 0.8f, 1.f);

    return false;
  }

  void setupScalarField(float*& scalar_field_d, const char* path, const uint3& field_size, cudaStream_t stream)
  {
    // Set up scalar field
    if (!path) {
      fprintf(stderr, "No input file specified.\n");
      exit(EXIT_FAILURE);
    }
    else if (strcmp("cayley", path) == 0) {
      buildCayleyField();
    }
    else if (!readFile(path)) {
      exit(EXIT_FAILURE);
    }
    assert(field_size.x * field_size.y * field_size.z * 4 == scalarField_host.size());
    fprintf(stderr, "Scalar field is [%d x %d x %d] (%d cells total)\n", field_size.x, field_size.y, field_size.z, field_size.x * field_size.y * field_size.z);
    CHECKED_CUDA(cudaMalloc(&scalar_field_d, scalarField_host.size()));
    CHECKED_CUDA(cudaMemcpyAsync(scalar_field_d, scalarField_host.data(), scalarField_host.size(), cudaMemcpyHostToDevice, stream));
  }

  void initWindowAndGL(GLFWwindow*& win, GLuint& shadedProg, GLuint& solidProg)
  {
    glfwSetErrorCallback(onGLFWError);
    if (!glfwInit()) {
      fprintf(stderr, "GLFW failed to initialize.\n");
      exit(EXIT_FAILURE);
    }
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    win = glfwCreateWindow(1280, 720, "Marching cubes test application", nullptr, nullptr);
    glfwSetKeyCallback(win, onKey);
    glfwMakeContextCurrent(win);
    gladLoadGL(glfwGetProcAddress);

    GLuint simpleVS = createShader(simpleVS_src, GL_VERTEX_SHADER);   assert(simpleVS != 0);
    GLuint simpleFS = createShader(simpleFS_src, GL_FRAGMENT_SHADER); assert(simpleFS != 0);
    shadedProg = createProgram(simpleVS, simpleFS);                   assert(shadedProg != 0);

    GLuint solidVS = createShader(solidVS_src, GL_VERTEX_SHADER);     assert(solidVS != 0);
    GLuint solidFS = createShader(solidFS_src, GL_FRAGMENT_SHADER);   assert(solidFS != 0);
    solidProg = createProgram(solidVS, solidFS);                      assert(solidPrg != 0);
  }

}

