1 module dcompute.driver.cuda.buffer;
2 
3 import dcompute.driver.cuda;
4 
5 struct Buffer(T)
6 {
7     size_t raw;
8 
9 	// Host memory associated with this buffer
10     T[] hostMemory;
11 
12     this(size_t elems)
13     {
14         status = cast(Status)cuMemAlloc(&raw,elems * T.sizeof);
15         checkErrors();
16         hostMemory = null;
17     }
18 
19     this(T[] arr)
20     {
21         status = cast(Status)cuMemAlloc(&raw,arr.length * T.sizeof);
22         checkErrors();
23         hostMemory = arr;
24     }
25     void copy(Copy c)()
26     {
27         static if (c == Copy.hostToDevice)
28         {
29             status = cast(Status)cuMemcpyHtoD(raw, hostMemory.ptr,hostMemory.length * T.sizeof);
30         }
31         else static if  (c == Copy.deviceToHost)
32         {
33             status = cast(Status)cuMemcpyDtoH(hostMemory.ptr,raw,hostMemory.length * T.sizeof);
34         }
35         checkErrors();
36     }
37     alias hostArgOf(U : GlobalPointer!T) = raw; 
38     void release()
39     {
40         status = cast(Status)cuMemFree(raw);
41         checkErrors();
42         raw = 0;
43         hostMemory = null;
44     }
45 }
46 
47 alias bf = Buffer!float;