Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added OpenCL/OpenGL particle system, moved android code to android fo…

…lder to make this an overall particle system project
  • Loading branch information...
commit d710f28b0b40f1651e919d8eec0cd84e8cf8efe8 1 parent 80e926a
@enjalot authored
Showing with 2,171 additions and 41 deletions.
  1. +24 −41 README
  2. 0  { → android}/AndroidManifest.xml
  3. +41 −0 android/README
  4. 0  { → android}/build.properties
  5. 0  { → android}/build.xml
  6. 0  { → android}/default.properties
  7. 0  { → android}/gen/com/enja/particles/R.java
  8. 0  { → android}/jni/Android.mk
  9. 0  { → android}/jni/app-android.c
  10. 0  { → android}/jni/app.h
  11. 0  { → android}/jni/demo.c
  12. 0  { → android}/jni/enja.c
  13. 0  { → android}/jni/importgl.c
  14. 0  { → android}/jni/importgl.h
  15. 0  { → android}/local.properties
  16. 0  { → android}/res/drawable-hdpi/icon.png
  17. 0  { → android}/res/drawable-ldpi/icon.png
  18. 0  { → android}/res/drawable-mdpi/icon.png
  19. 0  { → android}/res/layout/main.xml
  20. 0  { → android}/res/values/strings.xml
  21. 0  { → android}/src/com/enja/particles/EnjaParticles.java
  22. +152 −0 cmake/COMMON_CONFIG.cmake
  23. +47 −0 cmake/FindGLEW.cmake
  24. +50 −0 cmake/FindOPENCL.cmake
  25. +106 −0 cmake/OSX_CONFIG.cmake
  26. +122 −0 cmake/UBUNTU_CONFIG.cmake
  27. +32 −0 cmake/cmake_standalone.txt
  28. +1 −0  enjacl/.svnignore
  29. +56 −0 enjacl/CMakeLists.txt
  30. +55 −0 enjacl/README
  31. +61 −0 enjacl/enja.cl
  32. +149 −0 enjacl/enja.cpp
  33. +71 −0 enjacl/enja.h
  34. +28 −0 enjacl/incopencl.h
  35. +193 −0 enjacl/main.cpp
  36. +456 −0 enjacl/main_standalone.cpp
  37. +241 −0 enjacl/opencl.cpp
  38. +209 −0 enjacl/util.cpp
  39. +14 −0 enjacl/util.h
  40. +32 −0 environment/setupenv_enjmac
  41. +31 −0 environment/setupenv_vislab
View
65 README
@@ -1,41 +1,24 @@
-Simple OpenGL Particle system written in C
-
-No physics yet... just figured out enough interaction between Java and C to get to work
-
-built against android-7 (must have 2.1 to run)
-to build:
-#build the C files (found in jni folder)
-ndk-build
-#build the java stuff
-ant debug
-#move to phone or use -e instead of -d for emulator
-adb -d install bin/EnjaParticles-debug.apk
-
-I use ddms command to capture log output from my app, just add a tag filter for EnjaParticles so you can get useful output
-
-#Particle System
-particles are emitted from a generator (initialized as a static array)
-several static arrays govern the behavior of the particles
-the position of the particles is stored in static vertices array
- vertices array is used to draw the particles
- colors array determines color of each particle, with seperate alpha array that updates it
-
-#Weird stuff I don't get yet:
-When calling a function defined in enja.c from app-android.c I can't pass floating point values,
-I had to pass a pointer to static floats instead. integers work just fine, I believe it's something to
-do with the JNI but I know too little about it.
-I'm not really happy with the way the code is laid out, the particle system should probably be its own library
-with all the application stuff in the native function area. I'm not that used to C and playing with statics
-Doing this in C++ might make design a bit easier.
-
-
-#TODO
-//make an actual particle system
- better paramaterization for elements of the system
- make consistant way for variables to depend on time
- add physical interaction
-2d texture effects
-timing to measure costs of effects and compare devices
-rewrite in c++? maybe I can avoid weird JNI hack for passing floats
-write a version for OpengGL ES 2.0 with shaders to make pretty effects
-get OpenCL running on a phone ;D
+
+*******************************************************************
+ ### EnjaParticles in C++ using OpenCL and OpenGL interoperability
+
+enjacl/
+ the particle system library as well as a main.cpp to run it
+ check the README in here for build instructions and more info
+cmake/
+ cmake modules to help find necessary libraries
+
+environment/
+ scripts to set the environment variables that cmake needs
+
+
+
+*******************************************************************
+ ### EnjaParticles on the Android using the NDK and OpenGL
+
+android/
+ the C code is in the jni folder
+ read the README in this folder for more info
+
+
+*******************************************************************
View
0  AndroidManifest.xml → android/AndroidManifest.xml
File renamed without changes
View
41 android/README
@@ -0,0 +1,41 @@
+Simple OpenGL Particle system written in C
+
+No physics yet... just figured out enough interaction between Java and C to get to work
+
+built against android-7 (must have 2.1 to run)
+to build:
+#build the C files (found in jni folder)
+ndk-build
+#build the java stuff
+ant debug
+#move to phone or use -e instead of -d for emulator
+adb -d install bin/EnjaParticles-debug.apk
+
+I use ddms command to capture log output from my app, just add a tag filter for EnjaParticles so you can get useful output
+
+#Particle System
+particles are emitted from a generator (initialized as a static array)
+several static arrays govern the behavior of the particles
+the position of the particles is stored in static vertices array
+ vertices array is used to draw the particles
+ colors array determines color of each particle, with seperate alpha array that updates it
+
+#Weird stuff I don't get yet:
+When calling a function defined in enja.c from app-android.c I can't pass floating point values,
+I had to pass a pointer to static floats instead. integers work just fine, I believe it's something to
+do with the JNI but I know too little about it.
+I'm not really happy with the way the code is laid out, the particle system should probably be its own library
+with all the application stuff in the native function area. I'm not that used to C and playing with statics
+Doing this in C++ might make design a bit easier.
+
+
+#TODO
+//make an actual particle system
+ better paramaterization for elements of the system
+ make consistant way for variables to depend on time
+ add physical interaction
+2d texture effects
+timing to measure costs of effects and compare devices
+rewrite in c++? maybe I can avoid weird JNI hack for passing floats
+write a version for OpengGL ES 2.0 with shaders to make pretty effects
+get OpenCL running on a phone ;D
View
0  build.properties → android/build.properties
File renamed without changes
View
0  build.xml → android/build.xml
File renamed without changes
View
0  default.properties → android/default.properties
File renamed without changes
View
0  gen/com/enja/particles/R.java → android/gen/com/enja/particles/R.java
File renamed without changes
View
0  jni/Android.mk → android/jni/Android.mk
File renamed without changes
View
0  jni/app-android.c → android/jni/app-android.c
File renamed without changes
View
0  jni/app.h → android/jni/app.h
File renamed without changes
View
0  jni/demo.c → android/jni/demo.c
File renamed without changes
View
0  jni/enja.c → android/jni/enja.c
File renamed without changes
View
0  jni/importgl.c → android/jni/importgl.c
File renamed without changes
View
0  jni/importgl.h → android/jni/importgl.h
File renamed without changes
View
0  local.properties → android/local.properties
File renamed without changes
View
0  res/drawable-hdpi/icon.png → android/res/drawable-hdpi/icon.png
File renamed without changes
View
0  res/drawable-ldpi/icon.png → android/res/drawable-ldpi/icon.png
File renamed without changes
View
0  res/drawable-mdpi/icon.png → android/res/drawable-mdpi/icon.png
File renamed without changes
View
0  res/layout/main.xml → android/res/layout/main.xml
File renamed without changes
View
0  res/values/strings.xml → android/res/values/strings.xml
File renamed without changes
View
0  src/com/enja/particles/EnjaParticles.java → ...src/com/enja/particles/EnjaParticles.java
File renamed without changes
View
152 cmake/COMMON_CONFIG.cmake
@@ -0,0 +1,152 @@
+###############################################
+# Build Options (Definitions and compiler flags)
+###############################################
+ # Used by ALL compilers
+ #ADD_DEFINITIONS(-g)
+ # Used by SPECIFIC compilers
+ # SET (CMAKE_CXX_FLAGS )
+
+
+###############################################
+# EXTENSIONS TO INCLUDE:
+###############################################
+ ENABLE_TESTING()
+ #INCLUDE (CPack)
+ #INCLUDE (UseDoxygen)
+ #FIND_PACKAGE (MPI)
+
+ FIND_PACKAGE (GLUT)
+ FIND_PACKAGE (OpenGL)
+ FIND_PACKAGE (OPENCL)
+
+###############################################
+# External dependency search paths
+###############################################
+ # Directories searched for headers ORDER does not matter.
+ # If a directory does not exist it is skipped
+ set ( GLIB ${CMAKE_CURRENT_SOURCE_DIR} )
+ set ( SWAN $ENV{SWAN} )
+
+###############################################
+# Locate Required Libraries
+###############################################
+ # Find library: find_library(<VAR> name1 [path1 path2 ...])
+ # These are from gordon_tests (should repackage as subdirectories here for redistribution)
+ #FIND_LIBRARY (utilities utilities PATHS ${GLIB}/utilities)
+ #FIND_LIBRARY (amira amira PATHS ${GLIB}/amira)
+
+ FIND_LIBRARY (swan_cuda swan_cuda PATHS ${SWAN}/lib)
+ FIND_LIBRARY (swan_ocl swan_ocl PATHS ${SWAN}/lib)
+
+ if (SWAN_CUDA)
+ set(swan_lib ${swan_cuda})
+ set(SWAN_TRANSLATE go_cuda.x)
+ set (GPU_INCLUDE cuda)
+ endif (SWAN_CUDA)
+
+ if (SWAN_OPENCL)
+ set(swan_lib ${swan_ocl})
+ set(SWAN_TRANSLATE go_opencl.x)
+ set (GPU_INCLUDE opencl)
+ endif (SWAN_OPENCL)
+
+ message(***** : GPU_INCLUDE: "${GPU_INCLUDE}" ****)
+
+
+ # Download and install Armadillo separately.
+ # Specify local installation dir here. If installed globally the dir is unnecessary.
+ #FIND_LIBRARY (armadillo armadillo PATHS /Users/erlebach/Documents/src/armadillo-0.6.12 /usr/local/lib64 ~/local/lib)
+
+ # Typically installed separately. Same rules as Armadillo (local dir here; global unecessary)
+ #FIND_LIBRARY(fftw fftw3 PATHS /Users/erlebach/Documents/src/fftw-3.2.2/.libs)
+
+
+
+ SET (INCLUDE_DIRS
+ .
+ ${GLIB}/utilities
+ ${GLIB}/graphics_utils
+ ${SWAN}/include
+ #${GLIB}/cuda_utilities
+ #${GLIB}/swan_utilities
+ )
+
+ INCLUDE_DIRECTORIES ( ${INCLUDE_DIRS} )
+
+###############################################
+# Setup MPI (needed later)
+###############################################
+
+#OPTION (USE_MPI "Enable/Disable parallel build and linking with MPI" ON)
+#IF (MPI_FOUND AND USE_MPI)
+ #SET (CMAKE_CXX_FLAGS ${MPI_COMPILE_FLAGS})
+ #SET (CMAKE_C_FLAGS ${MPI_COMPILE_FLAGS})
+ #SET (CMAKE_LINK_FLAGS ${MPI_LINK_FLAGS})
+
+ #INCLUDE_DIRECTORIES (${MPI_INCLUDE_PATH})
+ # NOTE: add a target_link_library( MPI_LIBRARIES) for libs and bins
+ # TESTS that run parallel should use MPIEXEC
+#ENDIF (MPI_FOUND AND USE_MPI)
+
+
+###############################################
+# Allow 32bit compilation override
+# Written by Evan Bollig
+###############################################
+
+ OPTION(FORCE_32BIT "Force the compile and link process to build a 32bit i386 executable/library" ON)
+
+ SET(TEMP_VAR ${CMAKE_CURRENT_SOURCE_DIR}/lib )
+
+ IF (FORCE_32BIT)
+ # TEMPORARY (NOT worried about 32bit MPI right now)
+ SET(USE_MPI OFF)
+ MESSAGE("\n\nWARNING!!! Forcing MPI OFF (for 32bit build)\n\n")
+
+ # The C compiler is our linker
+ MESSAGE("Original Search Path: ${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
+
+ # EXTEND THE DEFAULT LIBRARY SEARCH PATH TO LOOK FOR i386
+ # ARCHITECTURE LIBRARIES
+ # /usr/lib32: for kirk (ubuntu)
+ SET(TEMP_VAR ${TEMP_VAR}/lib /usr/lib32 )
+ FOREACH(libdir ${CMAKE_C_IMPLICIT_LINK_DIRECTORIES})
+ # NOTE: ${OS_SPECIFIC_32BIT_IMPLICIT_LIB_SUBDIR}) is defined in the
+ # OS specific cmake builds (e.g., OSX_CONFIG.cmake)
+ SET(TEMP_VAR ${TEMP_VAR} ${libdir}/${OS_SPECIFIC_32BIT_IMPLICIT_LIB_SUBDIR})
+ SET(TEMP_VAR ${TEMP_VAR} ${libdir}${OS_SPECIFIC_32BIT_IMPLICIT_LIB_SUBDIR})
+ ENDFOREACH(libdir)
+
+ # THIS DOES NOT WORK!!!
+ # I WANT TO OVERRIDE THE IMPLICIT LINK DIRS FOR 32bit
+ # BUT THIS DOES NOT PROPAGATE TO THE LINKER CALL
+
+ # SET(CMAKE_C_IMPLICIT_LINK_DIRECTORIES ${TEMP_VAR})
+
+ # This works but gives warnings about the 64bit libraries
+ LINK_DIRECTORIES(${TEMP_VAR})
+
+ MESSAGE("Updated Search Path: ${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
+ #MESSAGE("CXX Implicit: ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES}")
+ #MESSAGE("FORTRAN Implicit: ${CMAKE_Fortran_IMPLICIT_LINK_DIRECTORIES}")
+
+ # FORCE COMPILERS INTO 32bit mode
+ ADD_DEFINITIONS( -m32 )
+ # FORCE LINKERS INTO 32bit mode
+ SET( CMAKE_EXE_LINKER_FLAGS -m32)
+
+ SET(LIB32_SEARCH_PATH ${TEMP_VAR} CACHE STRING "library search path")
+ ENDIF(FORCE_32BIT)
+
+
+
+# TEMPORARY. DO not know whether should be put in common
+IF($ENV{HOSTNAME} MATCHES "mark2")
+ FIND_PACKAGE(CUDA_mark2)
+ELSE($ENV{HOSTNAME} MATCHES "mark2")
+ FIND_PACKAGE(CUDA)
+ENDIF($ENV{HOSTNAME} MATCHES "mark2")
+
+FIND_LIBRARY(stdc stdc++ PATH /usr/lib32) #/gcc/x86_64-linux-gnu/4.4/32/)
+
+LINK_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/lib ${LINK_DIRECTORIES} )
View
47 cmake/FindGLEW.cmake
@@ -0,0 +1,47 @@
+#
+# Try to find GLEW library and include path.
+# Once done this will define
+#
+# GLEW_FOUND
+# GLEW_INCLUDE_PATH
+# GLEW_LIBRARY
+#
+
+IF (WIN32)
+ FIND_PATH( GLEW_INCLUDE_PATH GL/glew.h
+ $ENV{PROGRAMFILES}/GLEW/include
+ ${PROJECT_SOURCE_DIR}/src/nvgl/glew/include
+ DOC "The directory where GL/glew.h resides")
+ FIND_LIBRARY( GLEW_LIBRARY
+ NAMES glew GLEW glew32 glew32s
+ PATHS
+ $ENV{PROGRAMFILES}/GLEW/lib
+ ${PROJECT_SOURCE_DIR}/src/nvgl/glew/bin
+ ${PROJECT_SOURCE_DIR}/src/nvgl/glew/lib
+ DOC "The GLEW library")
+ELSE (WIN32)
+ FIND_PATH( GLEW_INCLUDE_PATH GL/glew.h
+ /usr/include
+ /usr/local/include
+ /sw/include
+ /opt/local/include
+ DOC "The directory where GL/glew.h resides")
+ FIND_LIBRARY( GLEW_LIBRARY
+ NAMES GLEW glew
+ PATHS
+ /usr/lib64
+ /usr/lib
+ /usr/local/lib64
+ /usr/local/lib
+ /sw/lib
+ /opt/local/lib
+ DOC "The GLEW library")
+ENDIF (WIN32)
+
+IF (GLEW_INCLUDE_PATH)
+ SET( GLEW_FOUND 1 CACHE STRING "Set to 1 if GLEW is found, 0 otherwise")
+ELSE (GLEW_INCLUDE_PATH)
+ SET( GLEW_FOUND 0 CACHE STRING "Set to 1 if GLEW is found, 0 otherwise")
+ENDIF (GLEW_INCLUDE_PATH)
+
+MARK_AS_ADVANCED( GLEW_FOUND )
View
50 cmake/FindOPENCL.cmake
@@ -0,0 +1,50 @@
+# - Try to find OpenCL
+# Once done this will define
+#
+# OPENCL_FOUND - system has OpenCL
+# OPENCL_INCLUDE_DIR - the OpenCL include directory
+# OPENCL_LIBRARIES - link these to use OpenCL
+#
+# WIN32 should work, but is untested
+
+IF (WIN32)
+
+ FIND_PATH(OPENCL_INCLUDE_DIR CL/cl.h )
+
+ # TODO this is only a hack assuming the 64 bit library will
+ # not be found on 32 bit system
+ FIND_LIBRARY(OPENCL_LIBRARIES opencl64 )
+ IF( OPENCL_LIBRARIES )
+ FIND_LIBRARY(OPENCL_LIBRARIES opencl32 )
+ ENDIF( OPENCL_LIBRARIES )
+
+ELSE (WIN32)
+
+ # Unix style platforms
+ # We also search for OpenCL in the NVIDIA GPU SDK default location
+ SET(OPENCL_INCLUDE_DIR "$ENV{OPENCL_HOME}/common/inc"
+ CACHE PATH "path to Opencl Include files")
+
+ message(***** OPENCL_INCLUDE_DIR: "${OPENCL_INCLUDE_DIR}" ********)
+
+ # does not work. WHY?
+ #SET(inc $ENV{CUDA_LOCAL}/../OpenCL/common/inc /usr/include)
+ #FIND_PATH(OPENCL_INCLUDE_DIR CL/cl.h PATHS ${inc} /usr/include )
+
+ FIND_LIBRARY(OPENCL_LIBRARIES OpenCL ENV LD_LIBRARY_PATH)
+
+ message(***** OPENCL ENV: "$ENV{GPU_SDK}" ********)
+
+#~/NVIDIA_GPU_Computing_SDK/OpenCL/common/inc/
+
+
+ENDIF (WIN32)
+
+SET( OPENCL_FOUND "NO" )
+IF(OPENCL_LIBRARIES )
+ SET( OPENCL_FOUND "YES" )
+ENDIF(OPENCL_LIBRARIES)
+
+MARK_AS_ADVANCED(
+ OPENCL_INCLUDE_DIR
+)
View
106 cmake/OSX_CONFIG.cmake
@@ -0,0 +1,106 @@
+FIND_LIBRARY (accelerate NAMES Accelerate)
+# CLAPACK is available from netlib.org or linux repositories
+# Use Accelerate library on OSX
+#FIND_LIBRARY (clapack clapack PATHS /usr/lib)
+
+
+#MESSAGE(STATUS "MacOS X detected. Added '-framework Accelerate' to compiler flags")
+
+# One or more dirs split by spaces. This is a command so it can be called multiple times
+INCLUDE_DIRECTORIES (AFTER
+)
+
+# One or more dirs split by spaces. This is a command so it can be called multiple times
+LINK_DIRECTORIES (
+)
+
+#FIND_LIBRARY(swan_ocl swan_ocl PATHS /Users/erlebach/Documents/src/fftw-3.2.2/.libs)
+#FIND_LIBRARY(swan_cuda swan_cuda PATHS ${SWAN}/lib)
+
+# Additional libraries required by this OS
+# NOTE: order of libraries is important in Linux.
+# does not matter on macOSX
+SET (ADDITIONAL_REQUIRED_LIBRARIES
+)
+
+#===================================================
+FIND_PACKAGE(Cuda)
+#CUDA_ADD_CUBLAS_TO_TARGET(target)
+#CUDA_ADD_EXECUTABLE(target file1 ...)
+#CUDA_ADD_LIBRARY(target file1 ...)
+#CUDA_BUILD_CLEAN_TARGET()
+#CUDA_INCLUDE_DIRECTORIES() # for nvcc
+set (CUDA_SDK_ROOT_DIR $ENV{CUDA_LOCAL}) # set environment variable
+message("${CUDA_TOOLKIT_ROOT_DIR}")
+set(CCC $ENV{CUDA_LOCAL}) # environment variable
+message("${CUDA_INCLUDE_DIRS}")
+message("${CUDA_VERSION_MAJOR}")
+message("${CUDA_VERSION_MINOR}")
+set(CUDA_64_BIT_DEVICE_CODE off)
+#set(CUDA_PROPAGATE_HOST_FLAGS off)
+
+
+# define environment variables
+message(sdk: "$ENV{CUDA_LOCAL}")
+message(sdk: "${CUDA_LOCAL}")
+message("${CCC}")
+FIND_PACKAGE(Cuda)
+#CUDA_ADD_CUBLAS_TO_TARGET(target)
+#CUDA_ADD_EXECUTABLE(target file1 ...)
+#CUDA_ADD_LIBRARY(target file1 ...)
+#CUDA_BUILD_CLEAN_TARGET()
+#CUDA_INCLUDE_DIRECTORIES() # for nvcc
+set (CUDA_SDK_ROOT_DIR $ENV{CUDA_LOCAL}) # set environment variable
+message("${CUDA_TOOLKIT_ROOT_DIR}")
+set(CCC $ENV{CUDA_LOCAL}) # environment variable
+message("${CUDA_INCLUDE_DIRS}")
+message("${CUDA_VERSION_MAJOR}")
+message("${CUDA_VERSION_MINOR}")
+set(CUDA_64_BIT_DEVICE_CODE off)
+#set(CUDA_NVCC_FLAGS -m32)
+#set(CUDA_PROPAGATE_HOST_FLAGS off)
+
+
+# define environment variables
+message(sdk: "$ENV{CUDA_LOCAL}")
+message(sdk: "${CUDA_LOCAL}")
+message("${CCC}")
+
+set(CUDA_NVCC_FLAGS -Xcompiler -m32)
+
+
+#---------------------
+if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ set(cuda_cutil_name cutil_x86_64)
+else(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ set(cuda_cutil_name cutil32)
+endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
+
+set(cuda_cutil_name cutil_i386)
+
+message("cutil name: ${cuda_cutil_name}")
+
+find_library(CUDA_CUT_LIBRARY
+ NAMES cutil ${cuda_cutil_name}
+ PATHS ${CUDA_SDK_ROOT_DIR}
+ # The new version of the sdk shows up in common/lib, but the old one is in lib
+ PATH_SUFFIXES "common/lib" "lib"
+ DOC "Location of cutil library"
+ NO_DEFAULT_PATH
+ )
+# Now search system paths
+find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
+ mark_as_advanced(CUDA_CUT_LIBRARY)
+ set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
+#---------------------
+
+
+
+
+
+# Mac is 64 bit, but must be compiled in 32 bit
+
+# for mac (gfortran)
+ADD_DEFINITIONS(-m32)
+
+#===================================================
View
122 cmake/UBUNTU_CONFIG.cmake
@@ -0,0 +1,122 @@
+#FIND_LIBRARY (accelerate NAMES Accelerate)
+# CLAPACK is available from netlib.org or linux repositories
+# Use Accelerate library on OSX
+#FIND_LIBRARY (clapack clapack PATHS /usr/lib)
+
+# required for graphics_libs/graphic_utils
+ADD_DEFINITIONS(-DLINUX)
+
+
+#MESSAGE(STATUS "MacOS X detected. Added '-framework Accelerate' to compiler flags")
+
+# One or more dirs split by spaces. This is a command so it can be called multiple times
+INCLUDE_DIRECTORIES (AFTER
+)
+
+# One or more dirs split by spaces. This is a command so it can be called multiple times
+LINK_DIRECTORIES (
+)
+
+#FIND_LIBRARY(swan_ocl swan_ocl PATHS /Users/erlebach/Documents/src/fftw-3.2.2/.libs)
+#FIND_LIBRARY(swan_cuda swan_cuda PATHS ${CMAKE_CURRENT_SOURCE_DIR}/swan/lib)
+
+# Additional libraries required by this OS
+# NOTE: order of libraries is important in Linux.
+# does not matter on macOSX
+SET (ADDITIONAL_REQUIRED_LIBRARIES
+)
+
+#set(CMAKE_MODULE_PATH "/usr/share/cmake-2.8/Modules;${CMAKE_MODULE_PATH}")
+
+message("PATH::: ${CMAKE_MODULE_PATH}")
+
+#===================================================
+message("after find_package cuda")
+#FindCUDA()
+#CUDA_ADD_CUBLAS_TO_TARGET(target)
+#CUDA_ADD_EXECUTABLE(target file1 ...)
+#CUDA_ADD_LIBRARY(target file1 ...)
+#CUDA_BUILD_CLEAN_TARGET()
+#CUDA_INCLUDE_DIRECTORIES() # for nvcc
+
+#Set paths before finding CUDA package
+set (CUDA_SDK_ROOT_DIR $ENV{CUDA_LOCAL}) # set environment variable
+set (CUDA_TOOLKIT_ROOT_DIR $ENV{CUDA_HOME}) # set environment variable
+FIND_PACKAGE(CUDA)
+
+#message("${CUDA_TOOLKIT_ROOT_DIR}")
+#message("${CUDA_SDK_ROOT_DIR}")
+
+#message("${CUDA_INCLUDE_DIRS}")
+#message("${CUDA_VERSION_MAJOR}")
+#message("${CUDA_VERSION_MINOR}")
+
+
+# define environment variables
+#message(sdk: "$ENV{CUDA_LOCAL}")
+#message(sdk: "${CUDA_LOCAL}")
+
+#CUDA_ADD_CUBLAS_TO_TARGET(target)
+#CUDA_ADD_EXECUTABLE(target file1 ...)
+#CUDA_ADD_LIBRARY(target file1 ...)
+#CUDA_BUILD_CLEAN_TARGET()
+#CUDA_INCLUDE_DIRECTORIES() # for nvcc
+
+set (CUDA_SDK_ROOT_DIR $ENV{CUDA_LOCAL}) # set environment variable
+
+#message("${CUDA_TOOLKIT_ROOT_DIR}")
+
+
+#if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ #set(cuda_cutil_name cutil_x86_64)
+#else(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ #set(cuda_cutil_name cutil32)
+#endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
+
+# always compile in 32 bit addressing
+set(cuda_cutil_name cutil_i386)
+
+message("cutil name: ${cuda_cutil_name}")
+
+find_library(CUDA_CUT_LIBRARY
+ NAMES cutil ${cuda_cutil_name}
+ PATHS ${CUDA_SDK_ROOT_DIR}
+ # The new version of the sdk shows up in common/lib, but the old one is in lib
+ PATH_SUFFIXES "common/lib" "lib"
+ DOC "Location of cutil library"
+ NO_DEFAULT_PATH
+ )
+# Now search system paths
+find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
+ mark_as_advanced(CUDA_CUT_LIBRARY)
+ set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
+
+
+
+
+
+
+#message("${CUDA_INCLUDE_DIRS}")
+#message("${CUDA_VERSION_MAJOR}")
+#message("${CUDA_VERSION_MINOR}")
+
+set(CUDA_64_BIT_DEVICE_CODE off)
+set(CUDA_NVCC_FLAGS -m32)
+#set(CUDA_PROPAGATE_HOST_FLAGS true)
+
+# This removes certain errors associated with __buildtin_stdarg_start that
+# are a part of g++ 4.4 . Even though my compiler is set to 4.3, some 4.4
+# includes are being called for reasons unknown.
+set(CUDA_NVCC_FLAGS -Xcompiler -D__builtin_stdarg_start=__builtin_va_start)
+#set(CUDA_NVCC_FLAGS -ccbin=/usr/bin/gcc-4.3 -c)
+
+#===================================================
+
+INCLUDE (CMakeForceCompiler)
+#SET(CMAKE_SYSTEM_NAME Generic)
+#CMAKE_FORCE_C_COMPILER (g++-4.3 4.3)
+#CMAKE_FORCE_CXX_COMPILER (g++-4.3 4.3)
+
+# Useful to search 32 bit libraries
+# lib/32/ or lib32/
+SET(OS_SPECIFIC_32BIT_IMPLICIT_LIB_SUBDIR 32)
View
32 cmake/cmake_standalone.txt
@@ -0,0 +1,32 @@
+
+
+###############################################
+# APPLE OSX 10.6
+###############################################
+IF(APPLE)
+ message("apple")
+ INCLUDE (OSX_CONFIG)
+
+ELSEIF($ENV{HOSTNAME} MATCHES "mark2")
+ message("mark2")
+ INCLUDE(MARK2_CONFIG)
+
+###############################################
+# UBUNTU LINUX 9.10
+# FSU Vislab
+###############################################
+ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+ message("ubuntu")
+ INCLUDE (UBUNTU_CONFIG)
+
+ELSE(APPLE)
+ message("Not implemented")
+
+ENDIF(APPLE)
+
+################# END OS DEPENDENT CONFIGS ####################
+
+
+# Get common build instructions used by all operating systems
+INCLUDE (COMMON_CONFIG)
+
View
1  enjacl/.svnignore
@@ -0,0 +1 @@
+build/
View
56 enjacl/CMakeLists.txt
@@ -0,0 +1,56 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+project(enjacl)
+
+#need to include FindOPENCL.cmake to
+SET( CMAKE_MODULE_PATH
+ ${enjacl_SOURCE_DIR}/../cmake
+ $ENV{CMAKE_MODULES})
+
+message("${CMAKE_MODULE_PATH}\n")
+
+
+FIND_PACKAGE (GLUT)
+FIND_PACKAGE (OpenGL)
+FIND_PACKAGE (OPENCL)
+FIND_PACKAGE (GLEW)
+
+INCLUDE_DIRECTORIES(
+ ${GLUT_INCLUDE_DIR}
+ ${OPENGL_INCLUDE_DIR}
+ ${GLEW_INCLUDE_PATH}
+ ${OPENCL_INCLUDE_DIR}
+)
+
+message("\nenjacl INCLUDE_DIRECTORIES\n")
+get_directory_property(includes INCLUDE_DIRECTORIES)
+message("${includes}\n")
+
+#provide the source path so we can open our .cl file at runtime
+ADD_DEFINITIONS(-DCL_SOURCE_DIR="${enjacl_SOURCE_DIR}")
+#make OpenCL use the OpenGL context to avoid memory transfer
+ADD_DEFINITIONS(-DGL_INTEROP)
+ADD_DEFINITIONS(-g )
+
+#particles opencl library
+set (CLCXXFILES enja.cpp opencl.cpp util.cpp)# timege.cpp)
+SET(ocllib enjaclparticles)
+ADD_LIBRARY(${ocllib} ${CLCXXFILES})
+
+
+SET(EXEC_CL oclenjaglcl.x)
+ADD_EXECUTABLE(${EXEC_CL} main.cpp util.cpp)
+
+TARGET_LINK_LIBRARIES (${EXEC_CL}
+ ${ocllib}
+ ${GLUT_LIBRARIES}
+ ${OPENGL_LIBRARIES}
+ ${OPENCL_LIBRARIES}
+ ${GLEW_LIBRARY}
+)
+
+
+#CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/enja.v.glsl ${CMAKE_CURRENT_BINARY_DIR}/enja.v.glsl)
+#CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/enja.f.glsl ${CMAKE_CURRENT_BINARY_DIR}/enja.f.glsl)
+#don't need to copy it if we provide path to source dir
+#CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/enja.cl ${CMAKE_CURRENT_BINARY_DIR}/enja.cl)
+
View
55 enjacl/README
@@ -0,0 +1,55 @@
+
+### EnjaParticles
+OpenCL accelerated particle system. The idea is that this library can take in a generator (emitter)
+and create an opencl/opengl context to compute and update a particle system stored in a VBO (vertex buffer object)
+
+Before long it should be inserted into the Blender Game Engine where it will act upon an object that has the Enja
+modifier (currently a simple custom modifer), use it's Mesh data as the generator and then display the particles.
+
+Collision detection on the GPU is the next goal after that.
+
+### Build
+#setup your environment by editing either setupenv_enjmac (for Mac OS X)
+#or setupenv_vislab (for ubuntu) so that you can point cmake to where
+#you have the NVIDIA SDK installed (for OpenCL headers and libs)
+#set the env variables like so:
+#(may want to add this to your .bashrc if you plan to build more often)
+source environment/setupenv_yourenv
+
+#Using cmake I prefer to do an out-of-source build:
+cd enjacl
+mkdir build
+cd build
+cmake ..
+make
+./oclenjacl.x
+
+
+
+### File breakdown
+enja.h:
+ defines EnjaParticle class
+
+enja.cpp:
+ defines the routines that are called by host program
+ executes opencl kernel with update routine
+
+opencl.cpp:
+ defines the opencl functions needed for EnjaParticle class
+
+incopencl.h:
+ include all the necessary OpenCL and OpenGL headers
+
+util.h/cpp:
+ function for opening .cl source files
+ 2 nvidia helper functions
+
+enja.cl:
+ OpenCL kernel functions which do the heavy lifting
+
+
+CMakeLists.txt:
+ cmake file for building the project, cross-platform tested on Ubuntu and Mac OS X.
+ eventually I'll have to support windows...
+
+
View
61 enjacl/enja.cl
@@ -0,0 +1,61 @@
+//update the particle position and color
+__kernel void enja(__global float4* vertices, __global float4* colors, __global float4* generators, __global float4* velocities, __global float* life, float dt)
+//__kernel void enja(__global float4* vertices, float dt)
+//__kernel void enja(__global float4* vertices, __global float4* colors, __global float4* generators, float dt)
+//__kernel void enja(__global float4* vertices, __global float4* colors, float dt)
+{
+ unsigned int i = get_global_id(0);
+
+
+ life[i] -= .005; //should probably depend on time somehow
+ if(life[i] <= 0.)
+ {
+ //reset this particle
+ vertices[i].x = generators[i].x;
+ vertices[i].y = generators[i].y;
+ vertices[i].z = generators[i].z;
+ float notrandom = .04f * dt / 5000.0f;//hardcoded based on runlength right now
+ velocities[i].y = .056 + notrandom; //not random but oh well
+ life[i] = 1.;
+ }
+
+ vertices[i].x += velocities[i].x;
+ vertices[i].y += velocities[i].y;
+ vertices[i].z += velocities[i].z;
+ //vertices[i].w += velocities[i].w;
+ velocities[i].y -= .0007; //this needs to depend on time or life
+
+ colors[i].y = life[i];
+ colors[i].z = life[i];
+ colors[i].w = 1-life[i];
+
+/*
+ vertices[i].x += dt;
+ colors[i].y += dt*10;
+ colors[i].z += dt*10;
+ colors[i].w -= dt*10;
+*/
+
+
+/*
+// old testing stuff
+ vertices[i].x = tv.x + 0.01f;
+ vertices[i].y = tv.y + 0.01f;
+ vertices[i].z = tv.z + 0.01f;
+ //vertices[i].w = 1.f;
+
+ colors[i].y = tc.y - 0.05f;
+ colors[i].z = tc.z - 0.05f;
+ if(colors[i].y <= 0.0f)
+ {
+ colors[i].y = 1.f;
+ }
+ if(colors[i].z <= 0.0f)
+ {
+ colors[i].z = 1.f;
+ }
+*/
+
+}
+
+
View
149 enjacl/enja.cpp
@@ -0,0 +1,149 @@
+#include <stdio.h>
+
+#include "enja.h"
+#include "util.h"
+
+#include<math.h>
+#include<stdlib.h>
+#include<time.h>
+
+int EnjaParticles::init(Vec4* g, Vec4* c, int n)
+{
+ num = n;
+ generators = g;
+ colors = c;
+
+ //initialize our vbos
+ vbo_size = sizeof(Vec4) * n;
+ v_vbo = createVBO(generators, vbo_size, GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
+ c_vbo = createVBO(colors, vbo_size, GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
+
+ srand48(time(NULL));
+
+ velocities = new Vec4[num];
+ for(int i=0; i < n; i++)
+ {
+ velocities[i].x = .01 * (1. - 2.*drand48()); // between -.02 and .02
+ velocities[i].y = .05 * drand48();
+ velocities[i].z = .01 * (1. - 2.*drand48());
+ velocities[i].w = 0.f;
+ }
+
+
+ life = new float[num];
+ for(int i=0; i < n; i++)
+ {
+ life[i] = drand48();
+ }
+
+
+ return 1;
+}
+
+
+EnjaParticles::EnjaParticles()
+{
+ printf("default constructor\n");
+ made_default = true; //need to remember to delete our allocated arrays
+ //init system
+ int n = 1000000;
+ Vec4* g = new Vec4[n];
+
+ float f = 0;
+ for(int i=0; i < n; i++)
+ {
+ f = (float)i;
+ g[i].x = 0.0 + .1*cos(2.*M_PI*(f/n));
+ //g[i].y = 0.0 + .05*sin(2.*M_PI*(f/n));
+ g[i].y = -1.0f;
+ g[i].z = 0.0 + .1*sin(2.*M_PI*(f/n));
+ //g[i].z = 0.f;// + f/nums;
+ g[i].w = 1.f;
+ }
+
+ Vec4* c = new Vec4[n];
+ for(int i=0; i < n; i++)
+ {
+ c[i].x = 1.0; //Red
+ c[i].y = 0.0; //Green
+ c[i].z = 0.0; //Blue
+ c[i].w = 1.0; //Alpha
+ }
+
+ printf("before init call\n");
+
+ //init particle system
+ init(g, c, n);
+
+ printf("before opencl call\n");
+
+ //init opencl
+ int success = init_cl();
+
+}
+
+EnjaParticles::EnjaParticles(Vec4* g, Vec4* c, int n)
+{
+ made_default = false;
+ //init particle system
+ init(g, c, n);
+
+ //init opencl
+ int success = init_cl();
+}
+
+EnjaParticles::~EnjaParticles()
+{
+
+ if (made_default)
+ {
+ delete generators;
+ delete colors;
+ }
+
+ delete velocities;
+ delete life;
+
+ if(ckKernel)clReleaseKernel(ckKernel);
+ if(cpProgram)clReleaseProgram(cpProgram);
+ if(cqCommandQueue)clReleaseCommandQueue(cqCommandQueue);
+ if(v_vbo)
+ {
+ glBindBuffer(1, v_vbo);
+ glDeleteBuffers(1, &v_vbo);
+ v_vbo = 0;
+ }
+ if(c_vbo)
+ {
+ glBindBuffer(1, c_vbo);
+ glDeleteBuffers(1, &c_vbo);
+ c_vbo = 0;
+ }
+ //if(vbo_cl)clReleaseMemObject(vbo_cl);
+ if(cl_vbos[0])clReleaseMemObject(cl_vbos[0]);
+ if(cl_vbos[1])clReleaseMemObject(cl_vbos[1]);
+ if(cl_generators)clReleaseMemObject(cl_generators);
+ if(cl_velocities)clReleaseMemObject(cl_velocities);
+ if(cl_life)clReleaseMemObject(cl_life);
+ if(cxGPUContext)clReleaseContext(cxGPUContext);
+
+ if(cdDevices)delete(cdDevices);
+
+}
+
+
+GLuint EnjaParticles::getVertexVBO()
+{
+ return v_vbo;
+}
+
+GLuint EnjaParticles::getColorVBO()
+{
+ return c_vbo;
+}
+
+
+int EnjaParticles::getNum()
+{
+ return num;
+}
View
71 enjacl/enja.h
@@ -0,0 +1,71 @@
+#ifndef ENJA_PARTICLES_H_INCLUDED
+#define ENJA_PARTICLES_H_INCLUDED
+
+#include "incopencl.h"
+
+typedef struct
+{
+ float x;
+ float y;
+ float z;
+ float w;
+} Vec4;
+
+
+
+
+class EnjaParticles
+{
+
+public:
+
+ int update(float dt);
+ GLuint getVertexVBO(); //get the vertices vbo id
+ GLuint getColorVBO(); //get the color vbo id
+ int getNum(); //get the number of particles
+
+ //constructors: will probably have more as more options are added
+ EnjaParticles();
+ EnjaParticles(Vec4* generators, Vec4* colors, int num);
+
+ ~EnjaParticles();
+
+private:
+ //particles
+ int num;
+ Vec4* generators;
+ Vec4* colors;
+ Vec4* velocities;
+ float* life;
+
+ int init(Vec4* generators, Vec4* colors, int num);
+
+ //opencl
+ cl_platform_id cpPlatform;
+ cl_context cxGPUContext;
+ cl_device_id* cdDevices;
+ cl_uint uiDevCount;
+ cl_command_queue cqCommandQueue;
+ cl_kernel ckKernel;
+ cl_program cpProgram;
+ cl_int ciErrNum;
+ size_t szGlobalWorkSize[1];
+
+ //cl_mem vbo_cl;
+ cl_mem cl_vbos[2]; //0: vertex vbo, 1: color vbo
+ cl_mem cl_generators; //want to have the start points for reseting particles
+ cl_mem cl_velocities; //particle velocities
+ cl_mem cl_life; //keep track where in their life the particles are
+ GLuint v_vbo; //vertices vbo
+ GLuint c_vbo; //colors vbo
+ unsigned int vbo_size; //size in bytes of the vbo
+
+
+ int init_cl();
+ void popCorn(); //purely convenient function to make init_cl shorter
+
+ bool made_default;
+
+};
+
+#endif
View
28 enjacl/incopencl.h
@@ -0,0 +1,28 @@
+#ifndef ENJA_INCOPENCL_H_INCLUDED
+#define ENJA_INCOPENCL_H_INCLUDED
+
+
+#include <GL/glew.h>
+#if defined __APPLE__ || defined(MACOSX)
+ //OpenGL stuff
+ #include <OpenGL/gl.h>
+ #include <OpenGL/glext.h>
+// #include <GLUT/glut.h>
+ #include <OpenGL/CGLCurrent.h> //is this really necessary?
+ //OpenCL stuff
+ #include <OpenCL/opencl.h>
+ #include <OpenCL/cl_gl.h>
+ #include <OpenCL/cl_gl_ext.h>
+ #define GL_SHARING_EXTENSION "cl_APPLE_gl_sharing"
+#else
+ //OpenGL stuff
+ #include <GL/glx.h>
+// #include <GL/glut.h>
+ //OpenCL stuff
+ #include <CL/opencl.h>
+ #include <CL/cl_gl.h>
+ #include <CL/cl_gl_ext.h>
+ #define GL_SHARING_EXTENSION "cl_khr_gl_sharing"
+#endif
+
+#endif
View
193 enjacl/main.cpp
@@ -0,0 +1,193 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+#include <time.h>
+
+#include <string.h>
+#include <string>
+
+#include <GL/glew.h>
+#if defined __APPLE__ || defined(MACOSX)
+ #include <GLUT/glut.h>
+#else
+ #include <GL/glut.h>
+ //OpenCL stuff
+#endif
+
+#include "enja.h"
+
+
+int window_width = 400;
+int window_height = 300;
+int glutWindowHandle = 0;
+float translate_z = -3.f;
+
+void init_gl();
+
+void appKeyboard(unsigned char key, int x, int y);
+void appRender();
+void appDestroy();
+
+EnjaParticles* enjas;
+int NUM_PARTICLES;
+
+GLuint v_vbo; //vbo id
+GLuint c_vbo; //vbo id
+
+void init_gl()
+{
+ // default initialization
+ glClearColor(0.0, 0.0, 0.0, 1.0);
+ glDisable(GL_DEPTH_TEST);
+
+ // viewport
+ glViewport(0, 0, window_width, window_height);
+
+ // projection
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluPerspective(60.0, (GLfloat)window_width / (GLfloat) window_height, 0.1, 10.0);
+
+ // set view matrix
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ glTranslatef(0.0, 0.0, translate_z);
+/*
+ glRotatef(rotate_x, 1.0, 0.0, 0.0);
+ glRotatef(rotate_y, 0.0, 1.0, 0.0);
+*/
+ return;
+
+}
+
+void appKeyboard(unsigned char key, int x, int y)
+{
+ switch(key)
+ {
+ case '\033': // escape quits
+ case '\015': // Enter quits
+ case 'Q': // Q quits
+ case 'q': // q (or escape) quits
+ // Cleanup up and quit
+ appDestroy();
+ break;
+ }
+}
+
+void appRender()
+{
+ //update the buffers with new vertices and colors
+ enjas->update(.0001);
+
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ //printf("render!\n");
+
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable(GL_POINT_SMOOTH);
+
+ glBindBuffer(GL_ARRAY_BUFFER, c_vbo);
+ glColorPointer(4, GL_FLOAT, 0, 0);
+
+ glBindBuffer(GL_ARRAY_BUFFER, v_vbo);
+ glVertexPointer(4, GL_FLOAT, 0, 0);
+
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glEnableClientState(GL_COLOR_ARRAY);
+
+ glColor3f(0,1,0);
+ glPointSize(10.);
+ glDrawArrays(GL_POINTS, 0, NUM_PARTICLES);
+
+ glDisableClientState(GL_COLOR_ARRAY);
+ glDisableClientState(GL_VERTEX_ARRAY);
+ glDisable(GL_POINT_SMOOTH);
+ glDisable(GL_BLEND);
+
+ glutSwapBuffers();
+// glutPostRedisplay();
+}
+
+void appDestroy()
+{
+
+ delete enjas;
+ if(glutWindowHandle)glutDestroyWindow(glutWindowHandle);
+
+ exit(0);
+}
+
+void timerCB(int ms)
+{
+ glutTimerFunc(ms, timerCB, ms);
+ glutPostRedisplay();
+}
+
+int main(int argc, char** argv)
+{
+
+ //initialize glut
+ glutInit(&argc, argv);
+ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
+ glutInitWindowSize(window_width, window_height);
+ glutInitWindowPosition (glutGet(GLUT_SCREEN_WIDTH)/2 - window_width/2,
+ glutGet(GLUT_SCREEN_HEIGHT)/2 - window_height/2);
+
+ glutWindowHandle = glutCreateWindow("EnjaParticles");
+
+ glutDisplayFunc(appRender); //main rendering function
+ glutTimerFunc(30, timerCB, 30);
+ glutKeyboardFunc(appKeyboard);
+/*
+ glutMouseFunc(appMouse);
+ glutMotionFunc(appMotion);
+*/
+
+ // initialize necessary OpenGL extensions
+ glewInit();
+ GLboolean bGLEW = glewIsSupported("GL_VERSION_2_0 GL_ARB_pixel_buffer_object");
+ printf("GLEW supported?: %d\n", bGLEW);
+
+ //initialize the OpenGL scene for rendering
+ init_gl();
+
+ printf("before we call enjas functions\n");
+
+ enjas = new EnjaParticles();
+ v_vbo = enjas->getVertexVBO();
+ c_vbo = enjas->getColorVBO();
+ NUM_PARTICLES = enjas->getNum();
+
+ glutMainLoop();
+
+ printf("doesn't happen does it\n");
+ appDestroy();
+ return 0;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
View
456 enjacl/main_standalone.cpp
@@ -0,0 +1,456 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+#include <time.h>
+
+#include <string.h>
+#include <string>
+
+#include <GL/glew.h>
+#if defined __APPLE__ || defined(MACOSX)
+ //OpenGL stuff
+ #include <OpenGL/gl.h>
+ #include <OpenGL/glext.h>
+ #include <GLUT/glut.h>
+ #include <OpenGL/CGLCurrent.h> //is this really necessary?
+ //OpenCL stuff
+ #include <OpenCL/opencl.h>
+ #include <OpenCL/cl_gl.h>
+ #include <OpenCL/cl_gl_ext.h>
+ #define GL_SHARING_EXTENSION "cl_APPLE_gl_sharing"
+#else
+ //OpenGL stuff
+ #include <GL/glx.h>
+ #include <GL/glut.h>
+ //OpenCL stuff
+ #include <CL/opencl.h>
+ #include <CL/cl_gl.h>
+ #include <CL/cl_gl_ext.h>
+ #define GL_SHARING_EXTENSION "cl_khr_gl_sharing"
+#endif
+
+#include "util.h"
+
+//particle stuff
+#define NUM_PARTICLES 1
+Vec4 vertices[NUM_PARTICLES];
+
+GLuint vbo = 0;
+unsigned int vbo_size = sizeof(Vec4) * NUM_PARTICLES;
+
+int window_width = 400;
+int window_height = 300;
+int glutWindowHandle = 0;
+float translate_z = -3.f;
+
+void init_gl();
+
+void appKeyboard(unsigned char key, int x, int y);
+void appRender();
+void appDestroy();
+
+//opencl stuff
+cl_platform_id cpPlatform;
+cl_context cxGPUContext;
+cl_device_id* cdDevices;
+cl_uint uiDevCount;
+cl_command_queue cqCommandQueue;
+cl_kernel ckKernel;
+cl_mem vbo_cl;
+cl_program cpProgram;
+cl_int ciErrNum;
+size_t szGlobalWorkSize[] = {NUM_PARTICLES};
+
+
+void init_cl();
+void runKernel();
+
+void init_cl()
+{
+ //Get the NVIDIA platform
+ ciErrNum = oclGetPlatformID(&cpPlatform);
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Get the number of GPU devices available to the platform
+ ciErrNum = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &uiDevCount);
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Create the device list
+ cdDevices = new cl_device_id [uiDevCount];
+ ciErrNum = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, uiDevCount, cdDevices, NULL);
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Get device requested on command line, if any
+ unsigned int uiDeviceUsed = 0;
+ unsigned int uiEndDev = uiDevCount - 1;
+
+ bool bSharingSupported = false;
+ for(unsigned int i = uiDeviceUsed; (!bSharingSupported && (i <= uiEndDev)); ++i)
+ {
+ size_t extensionSize;
+ ciErrNum = clGetDeviceInfo(cdDevices[i], CL_DEVICE_EXTENSIONS, 0, NULL, &extensionSize );
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+ if(extensionSize > 0)
+ {
+ char* extensions = (char*)malloc(extensionSize);
+ ciErrNum = clGetDeviceInfo(cdDevices[i], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize);
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+ std::string stdDevString(extensions);
+ free(extensions);
+
+ size_t szOldPos = 0;
+ size_t szSpacePos = stdDevString.find(' ', szOldPos); // extensions string is space delimited
+ while (szSpacePos != stdDevString.npos)
+ {
+ if( strcmp(GL_SHARING_EXTENSION, stdDevString.substr(szOldPos, szSpacePos - szOldPos).c_str()) == 0 )
+ {
+ // Device supports context sharing with OpenGL
+ uiDeviceUsed = i;
+ bSharingSupported = true;
+ break;
+ }
+ do
+ {
+ szOldPos = szSpacePos + 1;
+ szSpacePos = stdDevString.find(' ', szOldPos);
+ }
+ while (szSpacePos == szOldPos);
+ }
+ }
+ }
+
+ // Define OS-specific context properties and create the OpenCL context
+ //#if defined (__APPLE_CC__)
+ #if defined (__APPLE__) || defined(MACOSX)
+ CGLContextObj kCGLContext = CGLGetCurrentContext();
+ CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
+ cl_context_properties props[] =
+ {
+ CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup,
+ 0
+ };
+ cxGPUContext = clCreateContext(props, 0,0, NULL, NULL, &ciErrNum);
+ #else
+ #if defined WIN32 // Win32
+ cl_context_properties props[] =
+ {
+ CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(),
+ CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
+ CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
+ 0
+ };
+ cxGPUContext = clCreateContext(props, 1, &cdDevices[uiDeviceUsed], NULL, NULL, &ciErrNum);
+ #else
+ cl_context_properties props[] =
+ {
+ CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
+ CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
+ CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
+ 0
+ };
+ cxGPUContext = clCreateContext(props, 1, &cdDevices[uiDeviceUsed], NULL, NULL, &ciErrNum);
+ #endif
+ #endif
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Log device used (reconciled for requested requested and/or CL-GL interop capable devices, as applies)
+ //shrLog("Device # %u, ", uiDeviceUsed);
+ //oclPrintDevName(LOGBOTH, cdDevices[uiDeviceUsed]);
+
+ cqCommandQueue = clCreateCommandQueue(cxGPUContext, cdDevices[uiDeviceUsed], 0, &ciErrNum);
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Program Setup
+ int pl;
+ size_t program_length;
+ printf("open the program\n");
+ //char* cSourceCL = file_contents("enja.cl", &pl);
+
+ std::string path(CL_SOURCE_DIR);
+ path += "/enja.cl";
+ //printf("%s\n", path.c_str());
+ char* cSourceCL = file_contents(path.c_str(), &pl);
+ //char* cSourceCL = file_contents("/panfs/panasas1/users/idj03/research/iansvn/enjacl/build/enja.cl", &pl);
+ //printf("file: %s\n", cSourceCL);
+ program_length = (size_t)pl;
+ //shrCheckErrorEX(cSourceCL != NULL, shrTRUE, pCleanup);
+
+ // create the program
+ cpProgram = clCreateProgramWithSource(cxGPUContext, 1,
+ (const char **) &cSourceCL, &program_length, &ciErrNum);
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ printf("building the program\n");
+ // build the program
+ ciErrNum = clBuildProgram(cpProgram, 0, NULL, "-cl-fast-relaxed-math", NULL, NULL);
+ //ciErrNum = clBuildProgram(cpProgram, 0, NULL, NULL, NULL, NULL);
+ if (ciErrNum != CL_SUCCESS)
+ {
+ printf("houston we have a problem\n%s\n", oclErrorString(ciErrNum));
+ }
+
+ printf("program built\n");
+ ckKernel = clCreateKernel(cpProgram, "enja", &ciErrNum);
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+ printf("kernel made: %s\n", oclErrorString(ciErrNum));
+
+
+
+}
+
+
+
+void init_gl()
+{
+ // default initialization
+ glClearColor(0.0, 0.0, 0.0, 1.0);
+ glDisable(GL_DEPTH_TEST);
+
+ // viewport
+ glViewport(0, 0, window_width, window_height);
+
+ // projection
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluPerspective(60.0, (GLfloat)window_width / (GLfloat) window_height, 0.1, 10.0);
+
+ // set view matrix
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ glTranslatef(0.0, 0.0, translate_z);
+/*
+ glRotatef(rotate_x, 1.0, 0.0, 0.0);
+ glRotatef(rotate_y, 0.0, 1.0, 0.0);
+*/
+ return;
+
+}
+
+void appKeyboard(unsigned char key, int x, int y)
+{
+ switch(key)
+ {
+ case '\033': // escape quits
+ case '\015': // Enter quits
+ case 'Q': // Q quits
+ case 'q': // q (or escape) quits
+ // Cleanup up and quit
+ appDestroy();
+ break;
+ }
+}
+
+GLuint createVBO(const void* data, int dataSize, GLenum target, GLenum usage)
+{
+ GLuint id = 0; // 0 is reserved, glGenBuffersARB() will return non-zero id if success
+
+ glGenBuffers(1, &id); // create a vbo
+ glBindBuffer(target, id); // activate vbo id to use
+ //glBufferData(target, dataSize, data, usage); // upload data to video card
+ glBufferData(GL_ARRAY_BUFFER, vbo_size, vertices, GL_DYNAMIC_DRAW); // upload data to video card
+
+ // check data size in VBO is same as input array, if not return 0 and delete VBO
+ int bufferSize = 0;
+ glGetBufferParameteriv(target, GL_BUFFER_SIZE, &bufferSize);
+ if(dataSize != bufferSize)
+ {
+ glDeleteBuffers(1, &id);
+ id = 0;
+ //cout << "[createVBO()] Data size is mismatch with input array\n";
+ printf("[createVBO90] Data size is mismatch with input array\n");
+ }
+
+ return id; // return VBO id
+}
+
+
+void appRender()
+{
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ //printf("render!\n");
+
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable(GL_POINT_SMOOTH);
+
+ //update the buffers with new vertices and colors
+
+ //glBindBuffer(GL_ARRAY_BUFFER, glres.vertex_buffer);
+ //glBindBuffer(GL_ARRAY_BUFFER, glres.color_buffer);
+ //glBufferData(GL_ARRAY_BUFFER, glres.vbo_size, colors, GL_DYNAMIC_DRAW);
+ //glColorPointer(4, GL_FLOAT, 0, 0);
+
+ runKernel();
+
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ //glBufferData(GL_ARRAY_BUFFER, vbo_size, vertices, GL_DYNAMIC_DRAW);
+ glVertexPointer(4, GL_FLOAT, 0, 0);
+/*
+ void* ptr = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_ONLY_ARB);
+ Vec4 tmp = ((Vec4*)ptr)[0];
+ printf("VBO coord[0] %g %g %g\n", tmp.x, tmp.y, tmp.z);
+ glUnmapBufferARB(GL_ARRAY_BUFFER);
+*/
+
+ glEnableClientState(GL_VERTEX_ARRAY);
+ //glEnableClientState(GL_COLOR_ARRAY);
+ glColor3f(0,1,0);
+ glPointSize(10.);
+ glDrawArrays(GL_POINTS, 0, NUM_PARTICLES);
+// glDisableClientState(GL_COLOR_ARRAY);
+ glDisableClientState(GL_VERTEX_ARRAY);
+ glDisable(GL_POINT_SMOOTH);
+ glDisable(GL_BLEND);
+
+
+
+ glutSwapBuffers();
+ glutPostRedisplay();
+}
+
+void appDestroy()
+{
+
+ if(ckKernel)clReleaseKernel(ckKernel);
+ if(cpProgram)clReleaseProgram(cpProgram);
+ if(cqCommandQueue)clReleaseCommandQueue(cqCommandQueue);
+ if(vbo)
+ {
+ glBindBuffer(1, vbo);
+ glDeleteBuffers(1, &vbo);
+ vbo = 0;
+ }
+ if(vbo_cl)clReleaseMemObject(vbo_cl);
+ if(cxGPUContext)clReleaseContext(cxGPUContext);
+
+ if(cdDevices)delete(cdDevices);
+
+ if(glutWindowHandle)glutDestroyWindow(glutWindowHandle);
+
+ exit(0);
+}
+
+void runKernel()
+{
+ ciErrNum = CL_SUCCESS;
+
+#ifdef GL_INTEROP
+ // map OpenGL buffer object for writing from OpenCL
+ glFinish();
+ ciErrNum = clEnqueueAcquireGLObjects(cqCommandQueue, 1, &vbo_cl, 0,0,0);
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+#endif
+
+ float anim = 0.0001;
+ ciErrNum = clSetKernelArg(ckKernel, 1, sizeof(float), &anim);
+ ciErrNum |= clEnqueueNDRangeKernel(cqCommandQueue, ckKernel, 1, NULL, szGlobalWorkSize, NULL, 0,0,0 );
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+#ifdef GL_INTEROP
+ // unmap buffer object
+ ciErrNum = clEnqueueReleaseGLObjects(cqCommandQueue, 1, &vbo_cl, 0,0,0);
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+ clFinish(cqCommandQueue);
+#else
+
+ // Explicit Copy
+ // map the PBO to copy data from the CL buffer via host
+ glBindBufferARB(GL_ARRAY_BUFFER, vbo);
+
+ // map the buffer object into client's memory
+ void* ptr = glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY_ARB);
+
+ ciErrNum = clEnqueueReadBuffer(cqCommandQueue, vbo_cl, CL_TRUE, 0, vbo_size, ptr, 0, NULL, NULL);
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ glUnmapBufferARB(GL_ARRAY_BUFFER);
+#endif
+}
+
+
+int main(int argc, char** argv)
+{
+
+ //initialize glut
+ glutInit(&argc, argv);
+ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
+ glutInitWindowSize(window_width, window_height);
+ glutInitWindowPosition (glutGet(GLUT_SCREEN_WIDTH)/2 - window_width/2,
+ glutGet(GLUT_SCREEN_HEIGHT)/2 - window_height/2);
+
+ glutWindowHandle = glutCreateWindow("EnjaParticles");
+
+ glutDisplayFunc(appRender); //main rendering function
+ glutKeyboardFunc(appKeyboard);
+/*
+ glutMouseFunc(appMouse);
+ glutMotionFunc(appMotion);
+*/
+
+ // initialize necessary OpenGL extensions
+ glewInit();
+ GLboolean bGLEW = glewIsSupported("GL_VERSION_2_0 GL_ARB_pixel_buffer_object");
+ printf("GLEW supported?: %d\n", bGLEW);
+
+ //initialize the OpenGL scene for rendering
+ init_gl();
+
+ vertices[0].x = 0.f;
+ vertices[0].y = 0.f;
+ vertices[0].z = 0.f;
+ vertices[0].w = 1.f;
+
+ vbo = createVBO(vertices, vbo_size, GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
+
+ init_cl();
+
+ #ifdef GL_INTEROP
+ printf("gl interop!\n");
+ // create OpenCL buffer from GL VBO
+ vbo_cl = clCreateFromGLBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, vbo, &ciErrNum);
+ //printf("wwwwttttffff\n");
+ //printf("SUCCES?: %s\n", oclErrorString(ciErrNum));
+ #else
+ printf("no gl interop!\n");
+ // create standard OpenCL mem buffer
+ vbo_cl = clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, vbo_size, NULL, &ciErrNum);
+ ciErrNum = clEnqueueWriteBuffer(cqCommandQueue, vbo_cl, CL_TRUE, 0, vbo_size, vertices, 0, NULL, NULL);
+ clFinish(cqCommandQueue);
+ #endif
+
+ ciErrNum = clSetKernelArg(ckKernel, 0, sizeof(cl_mem), (void *) &vbo_cl);
+
+
+ glutMainLoop();
+
+ printf("doesn't happen does it\n");
+ appDestroy();
+ return 0;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
View
241 enjacl/opencl.cpp
@@ -0,0 +1,241 @@
+#include <string.h>
+#include <string>
+
+#include "enja.h"
+#include "util.h"
+
+int EnjaParticles::update(float dt)
+{
+ cl_int ciErrNum = CL_SUCCESS;
+ cl_event evt; //can't do opencl visual profiler without passing an event
+
+
+#ifdef GL_INTEROP
+ // map OpenGL buffer object for writing from OpenCL
+ glFinish();
+ //ciErrNum = clEnqueueAcquireGLObjects(cqCommandQueue, 1, &vbo_cl, 0,0,0);
+ ciErrNum = clEnqueueAcquireGLObjects(cqCommandQueue, 2, cl_vbos, 0,NULL, &evt);
+ clReleaseEvent(evt);
+ //printf("gl interop, acquire: %s\n", oclErrorString(ciErrNum));
+#endif
+
+ ciErrNum = clSetKernelArg(ckKernel, 5, sizeof(float), &dt);
+ //ciErrNum = clSetKernelArg(ckKernel, 2, sizeof(float), &dt);
+ ciErrNum |= clEnqueueNDRangeKernel(cqCommandQueue, ckKernel, 1, NULL, szGlobalWorkSize, NULL, 0, NULL, &evt );
+ clReleaseEvent(evt);
+ //printf("enqueueue nd range kernel: %s\n", oclErrorString(ciErrNum));
+
+#ifdef GL_INTEROP
+ // unmap buffer object
+ //ciErrNum = clEnqueueReleaseGLObjects(cqCommandQueue, 1, &vbo_cl, 0,0,0);
+ ciErrNum = clEnqueueReleaseGLObjects(cqCommandQueue, 2, cl_vbos, 0, NULL, &evt);
+ clReleaseEvent(evt);
+ //printf("gl interop, acquire: %s\n", oclErrorString(ciErrNum));
+ clFinish(cqCommandQueue);
+#else
+
+ // Explicit Copy
+ glBindBufferARB(GL_ARRAY_BUFFER, v_vbo);
+ // map the buffer object into client's memory
+ void* ptr = glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY_ARB);
+ ciErrNum = clEnqueueReadBuffer(cqCommandQueue, cl_vbos[0], CL_TRUE, 0, vbo_size, ptr, 0, NULL, &evt);
+ clReleaseEvent(evt);
+ glUnmapBufferARB(GL_ARRAY_BUFFER);
+
+ glBindBufferARB(GL_ARRAY_BUFFER, c_vbo);
+ // map the buffer object into client's memory
+ ptr = glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY_ARB);
+ ciErrNum = clEnqueueReadBuffer(cqCommandQueue, cl_vbos[1], CL_TRUE, 0, vbo_size, ptr, 0, NULL, &evt);
+ clReleaseEvent(evt);
+ glUnmapBufferARB(GL_ARRAY_BUFFER);
+#endif
+
+}
+
+
+void EnjaParticles::popCorn()
+{
+ cl_event evt; //can't do opencl visual profiler without passing an event
+ //This is a purely internal helper function, all this code could easily be at the bottom of init_cl
+ //init_cl shouldn't change much, and this may
+ #ifdef GL_INTEROP
+ printf("gl interop!\n");
+ // create OpenCL buffer from GL VBO
+ cl_vbos[0] = clCreateFromGLBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, v_vbo, &ciErrNum);
+ cl_vbos[1] = clCreateFromGLBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, c_vbo, &ciErrNum);
+ //printf("SUCCES?: %s\n", oclErrorString(ciErrNum));
+ #else
+ printf("no gl interop!\n");
+ // create standard OpenCL mem buffer
+ cl_vbos[0] = clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, vbo_size, NULL, &ciErrNum);
+ cl_vbos[1] = clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, vbo_size, NULL, &ciErrNum);
+ //Since we don't get the data from OpenGL we have to manually push the CPU side data to the GPU
+ ciErrNum = clEnqueueWriteBuffer(cqCommandQueue, cl_vbos[0], CL_TRUE, 0, vbo_size, generators, 0, NULL, &evt);
+ clReleaseEvent(evt);
+ ciErrNum = clEnqueueWriteBuffer(cqCommandQueue, cl_vbos[1], CL_TRUE, 0, vbo_size, colors, 0, NULL, &evt);
+ clReleaseEvent(evt);
+ //make sure we are finished copying over before going on
+ #endif
+
+ //support arrays for the particle system
+ cl_generators = clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, vbo_size, NULL, &ciErrNum);
+ cl_velocities= clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, vbo_size, NULL, &ciErrNum);
+ cl_life = clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, sizeof(float) * num, NULL, &ciErrNum);
+
+ ciErrNum = clEnqueueWriteBuffer(cqCommandQueue, cl_generators, CL_TRUE, 0, vbo_size, generators, 0, NULL, &evt);
+ clReleaseEvent(evt);
+ ciErrNum = clEnqueueWriteBuffer(cqCommandQueue, cl_velocities, CL_TRUE, 0, vbo_size, velocities, 0, NULL, &evt);
+ clReleaseEvent(evt);
+ ciErrNum = clEnqueueWriteBuffer(cqCommandQueue, cl_life, CL_TRUE, 0, sizeof(float) * num, life, 0, NULL, &evt);
+ clReleaseEvent(evt);
+ clFinish(cqCommandQueue);
+
+
+ //printf("about to set kernel args\n");
+ ciErrNum = clSetKernelArg(ckKernel, 0, sizeof(cl_mem), (void *) &cl_vbos[0]); //vertices is first arguement to kernel
+ ciErrNum = clSetKernelArg(ckKernel, 1, sizeof(cl_mem), (void *) &cl_vbos[1]); //colors is second arguement to kernel
+ ciErrNum = clSetKernelArg(ckKernel, 2, sizeof(cl_mem), (void *) &cl_generators); //colors is second arguement to kernel
+ ciErrNum = clSetKernelArg(ckKernel, 3, sizeof(cl_mem), (void *) &cl_velocities); //colors is second arguement to kernel
+ ciErrNum = clSetKernelArg(ckKernel, 4, sizeof(cl_mem), (void *) &cl_life); //colors is second arguement to kernel
+
+}
+
+int EnjaParticles::init_cl()
+{
+
+ szGlobalWorkSize[0] = num; //set the workgroup size to number of particles
+
+ cl_int ciErrNum;
+ //Get the NVIDIA platform
+ ciErrNum = oclGetPlatformID(&cpPlatform);
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Get the number of GPU devices available to the platform
+ ciErrNum = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &uiDevCount);
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Create the device list
+ cdDevices = new cl_device_id [uiDevCount];
+ ciErrNum = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, uiDevCount, cdDevices, NULL);
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Get device requested on command line, if any
+ unsigned int uiDeviceUsed = 0;
+ unsigned int uiEndDev = uiDevCount - 1;
+
+ bool bSharingSupported = false;
+ for(unsigned int i = uiDeviceUsed; (!bSharingSupported && (i <= uiEndDev)); ++i)
+ {
+ size_t extensionSize;
+ ciErrNum = clGetDeviceInfo(cdDevices[i], CL_DEVICE_EXTENSIONS, 0, NULL, &extensionSize );
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+ if(extensionSize > 0)
+ {
+ char* extensions = (char*)malloc(extensionSize);
+ ciErrNum = clGetDeviceInfo(cdDevices[i], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize);
+ //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+ std::string stdDevString(extensions);
+ free(extensions);
+
+ size_t szOldPos = 0;
+ size_t szSpacePos = stdDevString.find(' ', szOldPos); // extensions string is space delimited
+ while (szSpacePos != stdDevString.npos)
+ {
+ if( strcmp(GL_SHARING_EXTENSION, stdDevString.substr(szOldPos, szSpacePos - szOldPos).c_str()) == 0 )
+ {
+ // Device supports context sharing with OpenGL
+ uiDeviceUsed = i;
+ bSharingSupported = true;
+ break;
+ }
+ do
+ {
+ szOldPos = szSpacePos + 1;
+ szSpacePos = stdDevString.find(' ', szOldPos);
+ }
+ while (szSpacePos == szOldPos);
+ }
+ }
+ }
+
+ // Define OS-specific context properties and create the OpenCL context
+ //#if defined (__APPLE_CC__)
+ #if defined (__APPLE__) || defined(MACOSX)
+ CGLContextObj kCGLContext = CGLGetCurrentContext();
+ CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
+ cl_context_properties props[] =
+ {
+ CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup,
+ 0
+ };
+ cxGPUContext = clCreateContext(props, 0,0, NULL, NULL, &ciErrNum);
+ #else
+ #if defined WIN32 // Win32
+ cl_context_properties props[] =
+ {
+ CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(),
+ CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
+ CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
+ 0
+ };
+ cxGPUContext = clCreateContext(props, 1, &cdDevices[uiDeviceUsed], NULL, NULL, &ciErrNum);
+ #else
+ cl_context_properties props[] =
+ {
+ CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
+ CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
+ CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
+ 0
+ };
+ cxGPUContext = clCreateContext(props, 1, &cdDevices[uiDeviceUsed], NULL, NULL, &ciErrNum);
+ #endif
+ #endif
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Log device used (reconciled for requested requested and/or CL-GL interop capable devices, as applies)
+ //shrLog("Device # %u, ", uiDeviceUsed);
+ //oclPrintDevName(LOGBOTH, cdDevices[uiDeviceUsed]);
+
+ cqCommandQueue = clCreateCommandQueue(cxGPUContext, cdDevices[uiDeviceUsed], 0, &ciErrNum);
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ // Program Setup
+ int pl;
+ size_t program_length;
+ printf("open the program\n");
+ //char* cSourceCL = file_contents("enja.cl", &pl);
+
+ std::string path(CL_SOURCE_DIR);
+ path += "/enja.cl";
+ //printf("%s\n", path.c_str());
+ char* cSourceCL = file_contents(path.c_str(), &pl);
+ //char* cSourceCL = file_contents("/panfs/panasas1/users/idj03/research/iansvn/enjacl/build/enja.cl", &pl);
+ //printf("file: %s\n", cSourceCL);
+ program_length = (size_t)pl;
+ //shrCheckErrorEX(cSourceCL != NULL, shrTRUE, pCleanup);
+
+ // create the program
+ cpProgram = clCreateProgramWithSource(cxGPUContext, 1,
+ (const char **) &cSourceCL, &program_length, &ciErrNum);
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+
+ printf("building the program\n");
+ // build the program
+ ciErrNum = clBuildProgram(cpProgram, 0, NULL, "-cl-fast-relaxed-math", NULL, NULL);
+ //ciErrNum = clBuildProgram(cpProgram, 0, NULL, NULL, NULL, NULL);
+ if (ciErrNum != CL_SUCCESS)
+ {
+ printf("houston we have a problem\n%s\n", oclErrorString(ciErrNum));
+ }
+
+ printf("program built\n");
+ ckKernel = clCreateKernel(cpProgram, "enja", &ciErrNum);
+ //shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
+ printf("kernel made: %s\n", oclErrorString(ciErrNum));
+
+ popCorn();
+
+ return 1;
+}
+
+
View
209 enjacl/util.cpp
@@ -0,0 +1,209 @@
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <string>
+
+
+#include "incopencl.h"
+#include "util.h"
+
+char *file_contents(const char *filename, int *length)
+{
+ FILE *f = fopen(filename, "r");
+ void *buffer;
+
+ if (!f) {
+ fprintf(stderr, "Unable to open %s for reading\n", filename);
+ return NULL;
+ }
+
+ fseek(f, 0, SEEK_END);
+ *length = ftell(f);
+ fseek(f, 0, SEEK_SET);
+
+ buffer = malloc(*length+1);
+ *length = fread(buffer, 1, *length, f);
+ fclose(f);
+ ((char*)buffer)[*length] = '\0';
+
+ return (char*)buffer;
+}
+
+
+GLuint createVBO(const void* data, int dataSize, GLenum target, GLenum usage)
+{
+ GLuint id = 0; // 0 is reserved, glGenBuffersARB() will return non-zero id if success
+
+ glGenBuffers(1, &id); // create a vbo
+ glBindBuffer(target, id); // activate vbo id to use
+ glBufferData(target, dataSize, data, usage); // upload data to video card
+
+ // check data size in VBO is same as input array, if not return 0 and delete VBO
+ int bufferSize = 0;
+ glGetBufferParameteriv(target, GL_BUFFER_SIZE, &bufferSize);
+ if(dataSize != bufferSize)
+ {
+ glDeleteBuffers(1, &id);
+ id = 0;
+ //cout << "[createVBO()] Data size is mismatch with input array\n";
+ printf("[createVBO90] Data size is mismatch with input array\n");
+ }
+
+ return id; // return VBO id
+}
+
+
+
+//NVIDIA's code
+//////////////////////////////////////////////////////////////////////////////
+//! Gets the platform ID for NVIDIA if available, otherwise default to platform 0
+//!
+//! @return the id
+//! @param clSelectedPlatformID OpenCL platform ID
+//////////////////////////////////////////////////////////////////////////////
+cl_int oclGetPlatformID(cl_platform_id* clSelectedPlatformID)
+{
+ char chBuffer[1024];
+ cl_uint num_platforms;
+ cl_platform_id* clPlatformIDs;
+ cl_int ciErrNum;
+ *clSelectedPlatformID = NULL;
+ cl_uint i = 0;
+
+ // Get OpenCL platform count
+ ciErrNum = clGetPlatformIDs (0, NULL, &num_platforms);
+ if (ciErrNum != CL_SUCCESS)
+ {
+ //shrLog(" Error %i in clGetPlatformIDs Call !!!\n\n", ciErrNum);
+ printf(" Error %i in clGetPlatformIDs Call !!!\n\n", ciErrNum);
+ return -1000;
+ }
+ else
+ {
+ if(num_platforms == 0)
+ {
+ //shrLog("No OpenCL platform found!\n\n");
+ printf("No OpenCL platform found!\n\n");
+ return -2000;
+ }
+ else
+ {
+ // if there's a platform or more, make space for ID's
+ if ((clPlatformIDs = (cl_platform_id*)malloc(num_platforms * sizeof(cl_platform_id))) == NULL)
+ {
+ //shrLog("Failed to allocate memory for cl_platform ID's!\n\n");
+ printf("Failed to allocate memory for cl_platform ID's!\n\n");
+ return -3000;
+ }
+
+ // get platform info for each platform and trap the NVIDIA platform if found
+ ciErrNum = clGetPlatformIDs (num_platforms, clPlatformIDs, NULL);
+ for(i = 0; i < num_platforms; ++i)
+ {
+ ciErrNum = clGetPlatformInfo (clPlatformIDs[i], CL_PLATFORM_NAME, 1024, &chBuffer, NULL);
+ if(ciErrNum == CL_SUCCESS)
+ {
+ if(strstr(chBuffer, "NVIDIA") != NULL)
+ {
+ *clSelectedPlatformID = clPlatformIDs[i];
+ break;
+ }
+ }
+ }
+
+ // default to zeroeth platform if NVIDIA not found
+ if(*clSelectedPlatformID == NULL)
+ {
+ //shrLog("WARNING: NVIDIA OpenCL platform not found - defaulting to first platform!\n\n");
+ printf("WARNING: NVIDIA OpenCL platform not found - defaulting to first platform!\n\n");
+ *clSelectedPlatformID = clPlatformIDs[0];
+ }
+
+ free(clPlatformIDs);
+ }
+ }
+
+ return CL_SUCCESS;
+}
+
+
+// Helper function to get error string
+// *********************************************************************
+const char* oclErrorString(cl_int error)
+{
+ static const char* errorString[] = {
+ "CL_SUCCESS",
+ "CL_DEVICE_NOT_FOUND",
+ "CL_DEVICE_NOT_AVAILABLE",
+ "CL_COMPILER_NOT_AVAILABLE",
+ "CL_MEM_OBJECT_ALLOCATION_FAILURE",
+ "CL_OUT_OF_RESOURCES",
+ "CL_OUT_OF_HOST_MEMORY",
+ "CL_PROFILING_INFO_NOT_AVAILABLE",
+ "CL_MEM_COPY_OVERLAP",
+ "CL_IMAGE_FORMAT_MISMATCH",
+ "CL_IMAGE_FORMAT_NOT_SUPPORTED",
+ "CL_BUILD_PROGRAM_FAILURE",
+ "CL_MAP_FAILURE",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "CL_INVALID_VALUE",
+ "CL_INVALID_DEVICE_TYPE",
+ "CL_INVALID_PLATFORM",
+ "CL_INVALID_DEVICE",
+ "CL_INVALID_CONTEXT",
+ "CL_INVALID_QUEUE_PROPERTIES",
+ "CL_INVALID_COMMAND_QUEUE",
+ "CL_INVALID_HOST_PTR",
+ "CL_INVALID_MEM_OBJECT",
+ "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR",
+ "CL_INVALID_IMAGE_SIZE",
+ "CL_INVALID_SAMPLER",
+ "CL_INVALID_BINARY",
+ "CL_INVALID_BUILD_OPTIONS",
+ "CL_INVALID_PROGRAM",
+ "CL_INVALID_PROGRAM_EXECUTABLE",
+ "CL_INVALID_KERNEL_NAME",
+ "CL_INVALID_KERNEL_DEFINITION",
+ "CL_INVALID_KERNEL",
+ "CL_INVALID_ARG_INDEX",
+ "CL_INVALID_ARG_VALUE",
+ "CL_INVALID_ARG_SIZE",
+ "CL_INVALID_KERNEL_ARGS",
+ "CL_INVALID_WORK_DIMENSION",
+ "CL_INVALID_WORK_GROUP_SIZE",
+ "CL_INVALID_WORK_ITEM_SIZE",
+ "CL_INVALID_GLOBAL_OFFSET",
+ "CL_INVALID_EVENT_WAIT_LIST",
+ "CL_INVALID_EVENT",
+ "CL_INVALID_OPERATION",
+ "CL_INVALID_GL_OBJECT",
+ "CL_INVALID_BUFFER_SIZE",
+ "CL_INVALID_MIP_LEVEL",
+ "CL_INVALID_GLOBAL_WORK_SIZE",
+ };
+
+ const int errorCount = sizeof(errorString) / sizeof(errorString[0]);
+
+ const int index = -error;
+
+ return (index >= 0 && index < errorCount) ? errorString[index] : "";
+
+}
+
View
14 enjacl/util.h
@@ -0,0 +1,14 @@
+//from http://www.songho.ca/opengl/gl_vbo.html
+
+#ifndef ENJA_UTIL_H_INCLUDED
+#define ENJA_UTIL_H_INCLUDED
+
+char *file_contents(const char *filename, int *length);
+
+GLuint createVBO(const void* data, int dataSize, GLenum target, GLenum usage);
+
+cl_int oclGetPlatformID(cl_platform_id* clSelectedPlatformID);
+const char* oclErrorString(cl_int error);
+
+
+#endif
View
32 environment/setupenv_enjmac
@@ -0,0 +1,32 @@
+
+# setup environment variables for kirk
+
+export BASE_DIR=${HOME}/code
+
+#export CUDA_HOME=/usr/local/cuda
+export GPU_SDK=/Developer/GPU\ Computing
+export CUDA_HOME=${GPU_SDK}
+export CUDA_LOCAL=${GPU_SDK}/C
+export OPENCL_HOME=${GPU_SDK}/OpenCL
+export GRAPHIC_LIBS_HOME=${BASE_DIR}/graphics_libs
+export GRAPHICS_LIBS=${GRAPHIC_LIBS_HOME}/builds/enjmac
+
+# the latest version of swan does not work on the mac
+#export SWAN=${BASE_DIR}/swan2
+#export SWAN=${BASE_DIR}/../../trunk/swan
+#export SWAN=${BASE_DIR}/swan
+#export PATH=$SWAN/bin:$PATH
+
+
+#export CUDA_PROFILE_CONFIG=./profile_config
+
+#export DYLD_LIBRARY_PATH=${SWAN}/lib/:${DYLD_LIBRARY_PATH}:${CUDA_LOCAL}/common/lib/darwin
+
+export FC=gfortran
+export FCFLAGS=-m32
+
+export CMAKE_MODULES=/opt/local/share/cmake-2.8/Modules/
+
+export CUDA_LIB_PATH=/usr/lib32
+export CUDART_PATH=${CUDA_HOME}/lib
+export CUBLAS_PATH=${CUDA_HOME}/lib
View
31 environment/setupenv_vislab
@@ -0,0 +1,31 @@
+# setup environment variables for vizlab machines
+
+
+#BASE_DIR=/panfs/panasas1/research/erlebach/idj03/graphics_libs
+#export GPU_SDK=${HOME}/NVIDIA_GPU_Computing_SDK
+#export CUDA_HOME=${HOME}/NVIDIA_CUDA_TOOLKIT_3.0
+#export CUDA_LOCAL=${HOME}/NVIDIA_CUDA_SDK_3.0/C
+
+export BASE_DIR=/panfs/panasas1/research/erlebach
+
+export GPU_SDK=${BASE_DIR}/NVIDIA_GPU_Computing_SDK
+export CUDA_HOME=${BASE_DIR}/NVIDIA_CUDA_TOOLKIT_3.0
+export CUDA_LOCAL=${BASE_DIR}/NVIDIA_CUDA_SDK_3.0/C
+export OPENCL_HOME=${GPU_SDK}/OpenCL
+export GRAPHIC_LIBS_HOME=${BASE_DIR}/idj03/graphics_libs
+export GRAPHICS_LIBS=${GRAPHIC_LIBS_HOME}/builds/laforge
+
+export CXX=/usr/bin/g++-4.3
+export CC=/usr/bin/gcc-4.3
+
+export FC=/usr/bin/gfortran
+
+export CMAKE_MODULES=/usr/share/cmake-2.8/Modules/
+
+#unset CUDA_INSTALL_PATH
+#unset CUDA_LIBS
+
+export CUDA_LIB_PATH=/usr/lib32
+export CUDART_PATH=${CUDA_HOME}/lib
+export CUBLAS_PATH=${CUDA_HOME}/lib
+

0 comments on commit d710f28

Please sign in to comment.
Something went wrong with that request. Please try again.