Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / GpuSoftBodySolvers / OpenCL / btSoftBodySolverVertexBuffer_OpenGL.h
1 /*\r
2 Bullet Continuous Collision Detection and Physics Library\r
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/\r
4 \r
5 This software is provided 'as-is', without any express or implied warranty.\r
6 In no event will the authors be held liable for any damages arising from the use of this software.\r
7 Permission is granted to anyone to use this software for any purpose, \r
8 including commercial applications, and to alter it and redistribute it freely, \r
9 subject to the following restrictions:\r
10 \r
11 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.\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\r
13 3. This notice may not be removed or altered from any source distribution.\r
14 */\r
15 \r
16 #ifndef BT_SOFT_BODY_SOLVER_VERTEX_BUFFER_OPENGL_H\r
17 #define BT_SOFT_BODY_SOLVER_VERTEX_BUFFER_OPENGL_H \r
18 \r
19 \r
20 #include "BulletSoftBody/btSoftBodySolverVertexBuffer.h"\r
21 #ifdef USE_MINICL\r
22         #include "MiniCL/cl.h"\r
23 #else //USE_MINICL\r
24         #ifdef __APPLE__\r
25                 #include <OpenCL/OpenCL.h>\r
26         #else\r
27                 #include <CL/cl.h>\r
28                 #include <CL/cl_gl.h>\r
29         #endif //__APPLE__\r
30 #endif//USE_MINICL\r
31 \r
32 \r
33 #ifdef _WIN32//for glut.h\r
34 #include <windows.h>\r
35 #endif\r
36 \r
37 //think different\r
38 #if defined(__APPLE__) && !defined (VMDMESA)\r
39 #include <OpenGL/OpenGL.h>\r
40 #include <OpenGL/gl.h>\r
41 #include <OpenGL/glu.h>\r
42 #include <GLUT/glut.h>\r
43 #else\r
44 \r
45 \r
46 #ifdef _WINDOWS\r
47 #include <windows.h>\r
48 #include <GL/gl.h>\r
49 #include <GL/glu.h>\r
50 #else\r
51 #include <GL/glut.h>\r
52 #endif //_WINDOWS\r
53 #endif //APPLE\r
54 \r
55 \r
56 \r
57 class btOpenGLInteropVertexBufferDescriptor : public btVertexBufferDescriptor\r
58 {\r
59 protected:\r
60         /** OpenCL context */\r
61         cl_context                      m_context;\r
62 \r
63         /** OpenCL command queue */\r
64         cl_command_queue        m_commandQueue;\r
65         \r
66         /** OpenCL interop buffer */\r
67         cl_mem m_buffer;\r
68 \r
69         /** VBO in GL that is the basis of the interop buffer */\r
70         GLuint m_openGLVBO;\r
71 \r
72 \r
73 public:\r
74         /**\r
75          * context is the OpenCL context this interop buffer will work in.\r
76          * queue is the command queue that kernels and data movement will be enqueued into.\r
77          * openGLVBO is the OpenGL vertex buffer data will be copied into.\r
78          * vertexOffset is the offset in floats to the first vertex.\r
79          * vertexStride is the stride in floats between vertices.\r
80          */\r
81         btOpenGLInteropVertexBufferDescriptor( cl_command_queue cqCommandQue, cl_context context, GLuint openGLVBO, int vertexOffset, int vertexStride )\r
82         {\r
83 #ifndef USE_MINICL\r
84                 cl_int ciErrNum = CL_SUCCESS;\r
85                 m_context = context;\r
86                 m_commandQueue = cqCommandQue;\r
87                 \r
88                 m_vertexOffset = vertexOffset;\r
89                 m_vertexStride = vertexStride;\r
90 \r
91                 m_openGLVBO = openGLVBO;\r
92                 \r
93                 m_buffer = clCreateFromGLBuffer(m_context, CL_MEM_WRITE_ONLY, openGLVBO, &ciErrNum);\r
94                 if( ciErrNum != CL_SUCCESS )\r
95                 {\r
96                         btAssert( 0 &&  "clEnqueueAcquireGLObjects(copySoftBodyToVertexBuffer)");\r
97                 }\r
98 \r
99                 m_hasVertexPositions = true;\r
100 #else\r
101                 btAssert(0);//MiniCL shouldn't get here\r
102 #endif\r
103         }\r
104 \r
105         /**\r
106          * context is the OpenCL context this interop buffer will work in.\r
107          * queue is the command queue that kernels and data movement will be enqueued into.\r
108          * openGLVBO is the OpenGL vertex buffer data will be copied into.\r
109          * vertexOffset is the offset in floats to the first vertex.\r
110          * vertexStride is the stride in floats between vertices.\r
111          * normalOffset is the offset in floats to the first normal.\r
112          * normalStride is the stride in floats between normals.\r
113          */\r
114         btOpenGLInteropVertexBufferDescriptor( cl_command_queue cqCommandQue, cl_context context, GLuint openGLVBO, int vertexOffset, int vertexStride, int normalOffset, int normalStride )\r
115         {\r
116 #ifndef USE_MINICL\r
117                 cl_int ciErrNum = CL_SUCCESS;\r
118                 m_context = context;\r
119                 m_commandQueue = cqCommandQue;\r
120                 \r
121                 m_openGLVBO = openGLVBO;\r
122                 \r
123                 m_buffer = clCreateFromGLBuffer(m_context, CL_MEM_WRITE_ONLY, openGLVBO, &ciErrNum);\r
124                 if( ciErrNum != CL_SUCCESS )\r
125                 {\r
126                         btAssert( 0 &&  "clEnqueueAcquireGLObjects(copySoftBodyToVertexBuffer)");\r
127                 }\r
128 \r
129                 m_vertexOffset = vertexOffset;\r
130                 m_vertexStride = vertexStride;\r
131                 m_hasVertexPositions = true;\r
132 \r
133                 m_normalOffset = normalOffset;\r
134                 m_normalStride = normalStride;\r
135                 m_hasNormals = true;\r
136 #else\r
137                 btAssert(0);\r
138 #endif //USE_MINICL\r
139                 \r
140         }\r
141 \r
142         virtual ~btOpenGLInteropVertexBufferDescriptor()\r
143         {\r
144                 clReleaseMemObject( m_buffer );\r
145         }\r
146 \r
147         /**\r
148          * Return the type of the vertex buffer descriptor.\r
149          */\r
150         virtual BufferTypes getBufferType() const\r
151         {\r
152                 return OPENGL_BUFFER;\r
153         }\r
154 \r
155         virtual cl_context getContext() const\r
156         {\r
157                 return m_context;\r
158         }\r
159 \r
160         virtual cl_mem getBuffer() const\r
161         {\r
162                 return m_buffer;\r
163         }       \r
164 };\r
165 \r
166 #endif // #ifndef BT_SOFT_BODY_SOLVER_VERTEX_BUFFER_OPENGL_H\r