Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / RigidBodyGpuPipeline / opencl / primitives / Adl / Adl.h
1 /*
2 Copyright (c) 2012 Advanced Micro Devices, Inc.  
3
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose, 
7 including commercial applications, and to alter it and redistribute it freely, 
8 subject to the following restrictions:
9
10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
13 */
14 //Originally written by Takahiro Harada
15
16
17 #ifndef ADL_H
18 #define ADL_H
19
20 #pragma warning( disable : 4996 )
21 #include <Adl/AdlConfig.h>
22 #include <Adl/AdlError.h>
23 #include <algorithm>
24
25 #ifndef max
26 #define max(a,b)            (((a) > (b)) ? (a) : (b))
27 #endif
28
29 #ifndef min
30 #define min(a,b)            (((a) < (b)) ? (a) : (b))
31 #endif
32
33 namespace adl
34 {
35
36 enum DeviceType
37 {
38         TYPE_CL = 0,
39         TYPE_DX11 = 1,
40         TYPE_HOST,
41 };
42
43
44 struct Device;
45
46 struct BufferBase
47 {
48         enum BufferType
49         {
50                 BUFFER,
51
52                 //      for dx
53                 BUFFER_CONST,
54                 BUFFER_STAGING,
55                 BUFFER_APPEND,
56                 BUFFER_RAW,
57                 BUFFER_W_COUNTER,
58                 BUFFER_INDEX,
59                 BUFFER_VERTEX,
60
61                 //      for cl
62                 BUFFER_ZERO_COPY,
63
64         };
65 };
66
67 class DeviceUtils
68 {
69         public:
70                 struct Config
71                 {
72                         enum DeviceType
73                         {
74                                 DEVICE_GPU,
75                                 DEVICE_CPU,
76                         };
77
78                         //      for CL
79                         enum DeviceVendor
80                         {
81                                 VD_AMD,
82                                 VD_INTEL,
83                                 VD_NV,
84                         };
85
86                         Config() : m_type(DEVICE_GPU), m_deviceIdx(0), m_vendor(VD_AMD){}
87
88                         DeviceType m_type;
89                         int m_deviceIdx;
90                         DeviceVendor m_vendor;
91                 };
92
93                 __inline
94                 static
95                 int getNDevices( DeviceType type );
96                 __inline
97                 static Device* allocate( DeviceType type, Config& cfg );
98                 __inline
99                 static void deallocate( Device* deviceData );
100                 __inline
101                 static void waitForCompletion( const Device* deviceData );
102 };
103
104 //==========================
105 //      DeviceData
106 //==========================
107 struct Kernel;
108
109 struct Device
110 {
111         typedef DeviceUtils::Config Config;
112
113         Device( DeviceType type ) : m_type( type ), m_memoryUsage(0)
114         {
115         }
116
117         virtual void* getContext() const { return 0; }
118         virtual void initialize(const Config& cfg){}
119         virtual void release(){}
120         virtual void waitForCompletion() const {}
121         virtual void getDeviceName( char nameOut[128] ) const {}
122         virtual Kernel* getKernel(const char* fileName, const char* funcName, const char* option = NULL, const char* src = NULL, bool cacheKernel = true ) const { ADLASSERT(0); return 0;}
123         virtual unsigned int getUsedMemory() const { return m_memoryUsage; }
124
125         DeviceType m_type;
126         unsigned int m_memoryUsage;
127 };
128
129 //==========================
130 //      Buffer
131 //==========================
132
133 template<typename T>
134 struct HostBuffer;
135 //      overload each deviceDatas
136 template<typename T>
137 struct Buffer : public BufferBase
138 {
139         __inline
140         Buffer();
141         __inline
142         Buffer(const Device* device, int nElems, BufferType type = BUFFER );
143         __inline
144         virtual ~Buffer();
145         
146         __inline
147         void setRawPtr( const Device* device, T* ptr, int size, BufferType type = BUFFER );
148         __inline
149         void allocate(const Device* device, int nElems, BufferType type = BUFFER );
150         __inline
151         void write(T* hostSrcPtr, int nElems, int dstOffsetNElems = 0);
152         __inline
153         void read(T* hostDstPtr, int nElems, int srcOffsetNElems = 0) const;
154         __inline
155         void write(Buffer<T>& src, int nElems);
156         __inline
157         void read(Buffer<T>& dst, int nElems) const;
158 //      __inline
159 //      Buffer<T>& operator = (const Buffer<T>& buffer);
160         __inline
161         int getSize() const { return m_size; }
162
163         DeviceType getType() const { ADLASSERT( m_device ); return m_device->m_type; }
164
165
166         const Device* m_device;
167         int m_size;
168         T* m_ptr;
169         //      for DX11
170         void* m_uav;
171         void* m_srv;
172         bool m_allocated;       //      todo. move this to a bit
173 };
174
175 class BufferUtils
176 {
177 public:
178         template<DeviceType TYPE, bool COPY, typename T>
179         __inline
180         static
181         typename Buffer<T>* map(const Device* device, const Buffer<T>* in, int copySize = -1);
182
183         template<bool COPY, typename T>
184         __inline
185         static
186         void unmap( Buffer<T>* native, const Buffer<T>* orig, int copySize = -1 );
187 };
188
189 //==========================
190 //      HostBuffer
191 //==========================
192 struct DeviceHost;
193
194 template<typename T>
195 struct HostBuffer : public Buffer<T>
196 {
197         __inline
198         HostBuffer():Buffer<T>(){}
199         __inline
200         HostBuffer(const Device* device, int nElems, BufferType type = BUFFER ) : Buffer<T>(device, nElems, type) {}
201 //      HostBuffer(const Device* deviceData, T* rawPtr, int nElems);
202
203
204         __inline
205         T& operator[](int idx);
206         __inline
207         const T& operator[](int idx) const;
208         __inline
209         T* begin() { return m_ptr; }
210
211         __inline
212         HostBuffer<T>& operator = (const Buffer<T>& device);
213 };
214
215 };
216
217 #include <Adl/AdlKernel.h>
218 #if defined(ADL_ENABLE_CL)
219         #include <Adl/CL/AdlCL.inl>
220 #endif
221 #if defined(ADL_ENABLE_DX11)
222         #include <Adl/DX11/AdlDX11.inl>
223 #endif
224
225 #include <Adl/Host/AdlHost.inl>
226 #include <Adl/AdlKernel.inl>
227 #include <Adl/Adl.inl>
228
229
230 #include <Adl/AdlStopwatch.h>
231
232 #include <Adl/Host/AdlStopwatchHost.inl>
233 #include <Adl/AdlStopwatch.inl>
234
235 #endif