/***** GCL Generated File *********************/
/* Automatically generated file, do not edit! */
/**********************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <dispatch/dispatch.h>
#include <OpenCL/opencl.h>
#include <OpenCL/gcl_priv.h>
#include "denoise_stack_cl_source.h"

static void initBlocks(void);

// Initialize static data structures
static block_kernel_pair pair_map[1] = {
      { NULL, NULL }
};

static block_kernel_map bmap = { 0, 1, initBlocks, pair_map };

// Block function
void (^denoise_stack_kernel)(const cl_ndrange *ndrange, cl_uint* w_ind, cl_uint* h_ind, cl_int w_ind_size, cl_int h_ind_size, cl_uint* offsets, cl_float* stacks, cl_float* weights, cl_float* table_2D, cl_float* img_mask, cl_int width, cl_int height, cl_float lambdaHard3D, cl_float sigma) =
^(const cl_ndrange *ndrange, cl_uint* w_ind, cl_uint* h_ind, cl_int w_ind_size, cl_int h_ind_size, cl_uint* offsets, cl_float* stacks, cl_float* weights, cl_float* table_2D, cl_float* img_mask, cl_int width, cl_int height, cl_float lambdaHard3D, cl_float sigma) {
  int err = 0;
  cl_kernel k = bmap.map[0].kernel;
  if (!k) {
    initBlocks();
    k = bmap.map[0].kernel;
  }
  if (!k)
    gcl_log_fatal("kernel denoise_stack does not exist for device");
  kargs_struct kargs;
  gclCreateArgsAPPLE(k, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 0, w_ind, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 1, h_ind, &kargs);
  err |= gclSetKernelArgAPPLE(k, 2, sizeof(w_ind_size), &w_ind_size, &kargs);
  err |= gclSetKernelArgAPPLE(k, 3, sizeof(h_ind_size), &h_ind_size, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 4, offsets, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 5, stacks, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 6, weights, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 7, table_2D, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 8, img_mask, &kargs);
  err |= gclSetKernelArgAPPLE(k, 9, sizeof(width), &width, &kargs);
  err |= gclSetKernelArgAPPLE(k, 10, sizeof(height), &height, &kargs);
  err |= gclSetKernelArgAPPLE(k, 11, sizeof(lambdaHard3D), &lambdaHard3D, &kargs);
  err |= gclSetKernelArgAPPLE(k, 12, sizeof(sigma), &sigma, &kargs);
  gcl_log_cl_fatal(err, "setting argument for denoise_stack failed");
  err = gclExecKernelAPPLE(k, ndrange, &kargs);
  gcl_log_cl_fatal(err, "Executing denoise_stack failed");
  gclDeleteArgsAPPLE(k, &kargs);
};

// Initialization functions
static void initBlocks(void) {
  const char* build_opts = "";
  static dispatch_once_t once;
  dispatch_once(&once,
    ^{ int err = gclBuildProgramBinaryAPPLE("OpenCL/denoise_stack_cl_source", "com.macphun.noiselessPro", &bmap, build_opts);
       if (!err) {
          assert(bmap.map[0].block_ptr == denoise_stack_kernel && "mismatch block");
          bmap.map[0].kernel = clCreateKernel(bmap.program, "denoise_stack", &err);
       }
     });
}

__attribute__((constructor))
static void RegisterMap(void) {
  gclRegisterBlockKernelMap(&bmap);
  bmap.map[0].block_ptr = denoise_stack_kernel;
}

