Blame view

src/mior/StructBuffer.java 1.09 KB
89f70c1ec   glaville   import current mc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  package mior;
  
  import java.nio.ByteBuffer;
  
  import mcmas.core.MCMContext;
  import mcmas.core.MCMMem;
  
  import org.jocl.struct.Buffers;
  import org.jocl.struct.SizeofStruct;
  import org.jocl.struct.Struct;
  
  public class StructBuffer<T extends Struct> {
  	
  	private final T[] data;
  	private final ByteBuffer buffer;
  	private final long byteSize;
  	private final MCMMem mem;
  	
  	public StructBuffer(MCMContext context, T... data) {
  		this.data = data;
  		this.buffer = Buffers.allocateBuffer(data);
  		Buffers.writeToBuffer(buffer, data);
  		this.byteSize = SizeofStruct.sizeof(data[0].getClass());
  		this.mem = context.newBuffer().Using(buffer).b();
  		//createBuffer(byteSize, OCL.MEM_RW, Pointer.to(buffer));
  	}
  	
  	public T[] getData() {
  		return data;
  	}
  	
  	public ByteBuffer getBuffer() {
  		return buffer;
  	}
  	
  	public long getByteSize() {
  		return byteSize;
  	}
  	
  	public MCMMem getMem() {
  		return mem;
  	}
  	
  	public void read() {
  		buffer.rewind();
  		Buffers.readFromBuffer(buffer, data);
  	}
  	
  	public void write() {
  		buffer.rewind();
  		Buffers.writeToBuffer(buffer, data);
  	}
  	
  	public void release() {
  		mem.release();
  	}
  
  }