clray

diff src/ocl.cc @ 1:0b0e4d18d53f

added non-buffer args
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 12 Jul 2010 07:00:19 +0300
parents 5767277e049f
children 41d6253492ad
line diff
     1.1 --- a/src/ocl.cc	Mon Jul 12 05:56:47 2010 +0300
     1.2 +++ b/src/ocl.cc	Mon Jul 12 07:00:19 2010 +0300
     1.3 @@ -140,7 +140,7 @@
     1.4  	prog = 0;
     1.5  	kernel = 0;
     1.6  	this->kname = kname;
     1.7 -	mbuf.resize(16);
     1.8 +	args.resize(16);
     1.9  	built = false;
    1.10  }
    1.11  
    1.12 @@ -154,9 +154,9 @@
    1.13  
    1.14  		clReleaseKernel(kernel);
    1.15  	}
    1.16 -	for(size_t i=0; i<mbuf.size(); i++) {
    1.17 -		if(mbuf[i]) {
    1.18 -			destroy_mem_buffer(mbuf[i]);
    1.19 +	for(size_t i=0; i<args.size(); i++) {
    1.20 +		if(args[i].type == ARGTYPE_MEM_BUF) {
    1.21 +			destroy_mem_buffer(args[i].v.mbuf);
    1.22  		}
    1.23  	}
    1.24  }
    1.25 @@ -192,7 +192,31 @@
    1.26  	return true;
    1.27  }
    1.28  
    1.29 -bool CLProgram::set_arg(int arg, int rdwr, size_t sz, void *ptr)
    1.30 +bool CLProgram::set_argi(int idx, int val)
    1.31 +{
    1.32 +	if((int)args.size() <= idx) {
    1.33 +		args.resize(idx + 1);
    1.34 +	}
    1.35 +
    1.36 +	CLArg *arg = &args[idx];
    1.37 +	arg->type = ARGTYPE_INT;
    1.38 +	arg->v.ival = val;
    1.39 +	return true;
    1.40 +}
    1.41 +
    1.42 +bool CLProgram::set_argf(int idx, float val)
    1.43 +{
    1.44 +	if((int)args.size() <= idx) {
    1.45 +		args.resize(idx + 1);
    1.46 +	}
    1.47 +
    1.48 +	CLArg *arg = &args[idx];
    1.49 +	arg->type = ARGTYPE_FLOAT;
    1.50 +	arg->v.fval = val;
    1.51 +	return true;
    1.52 +}
    1.53 +
    1.54 +bool CLProgram::set_arg_buffer(int idx, int rdwr, size_t sz, void *ptr)
    1.55  {
    1.56  	CLMemBuffer *buf;
    1.57  
    1.58 @@ -200,19 +224,20 @@
    1.59  		return false;
    1.60  	}
    1.61  
    1.62 -	if((int)mbuf.size() <= arg) {
    1.63 -		mbuf.resize(arg + 1);
    1.64 +	if((int)args.size() <= idx) {
    1.65 +		args.resize(idx + 1);
    1.66  	}
    1.67 -	mbuf[arg] = buf;
    1.68 +	args[idx].type = ARGTYPE_MEM_BUF;
    1.69 +	args[idx].v.mbuf = buf;
    1.70  	return true;
    1.71  }
    1.72  
    1.73  CLMemBuffer *CLProgram::get_arg_buffer(int arg)
    1.74  {
    1.75 -	if(arg < 0 || arg >= (int)mbuf.size()) {
    1.76 +	if(arg < 0 || arg >= (int)args.size() || args[arg].type != ARGTYPE_MEM_BUF) {
    1.77  		return 0;
    1.78  	}
    1.79 -	return mbuf[arg];
    1.80 +	return args[arg].v.mbuf;
    1.81  }
    1.82  
    1.83  bool CLProgram::build()
    1.84 @@ -236,22 +261,53 @@
    1.85  		return false;
    1.86  	}
    1.87  
    1.88 -	for(size_t i=0; i<mbuf.size(); i++) {
    1.89 -		if(!mbuf[i]) break;
    1.90 +	for(size_t i=0; i<args.size(); i++) {
    1.91 +		int err;
    1.92  
    1.93 -		int err;
    1.94 -		if((err = clSetKernelArg(kernel, i, sizeof mbuf[i]->mem, &mbuf[i]->mem)) != 0) {
    1.95 -			fprintf(stderr, "failed to bind kernel argument: %d (%d)\n", (int)i, err);
    1.96 -			clReleaseProgram(prog);
    1.97 -			clReleaseKernel(kernel);
    1.98 -			prog = 0;
    1.99 -			kernel = 0;
   1.100 -			return false;
   1.101 +		if(args[i].type == ARGTYPE_NONE) {
   1.102 +			break;
   1.103 +		}
   1.104 +
   1.105 +		switch(args[i].type) {
   1.106 +		case ARGTYPE_INT:
   1.107 +			if((err = clSetKernelArg(kernel, i, sizeof(int), &args[i].v.ival)) != 0) {
   1.108 +				fprintf(stderr, "failed to bind kernel argument: %d (%d)\n", (int)i, err);
   1.109 +				goto fail;
   1.110 +			}
   1.111 +			break;
   1.112 +
   1.113 +		case ARGTYPE_FLOAT:
   1.114 +			if((err = clSetKernelArg(kernel, i, sizeof(float), &args[i].v.fval)) != 0) {
   1.115 +				fprintf(stderr, "failed to bind kernel argument: %d (%d)\n", (int)i, err);
   1.116 +				goto fail;
   1.117 +			}
   1.118 +			break;
   1.119 +
   1.120 +		case ARGTYPE_MEM_BUF:
   1.121 +			{
   1.122 +				CLMemBuffer *mbuf = args[i].v.mbuf;
   1.123 +
   1.124 +				if((err = clSetKernelArg(kernel, i, sizeof mbuf->mem, &mbuf->mem)) != 0) {
   1.125 +					fprintf(stderr, "failed to bind kernel argument: %d (%d)\n", (int)i, err);
   1.126 +					goto fail;
   1.127 +				}
   1.128 +			}
   1.129 +			break;
   1.130 +
   1.131 +		default:
   1.132 +			break;
   1.133  		}
   1.134  	}
   1.135  
   1.136  	built = true;
   1.137  	return true;
   1.138 +
   1.139 +fail:
   1.140 +	clReleaseProgram(prog);
   1.141 +	clReleaseKernel(kernel);
   1.142 +	prog = 0;
   1.143 +	kernel = 0;
   1.144 +	return false;
   1.145  }
   1.146  
   1.147  bool CLProgram::run() const