Skip to content

Commit

Permalink
Fixes for CUDA use-case
Browse files Browse the repository at this point in the history
First fixes to revive CUDA. Still working on it. Shouldn't have broken
anything out of CUDA. But CUDA might not work, yet.
  • Loading branch information
tlorach committed Dec 1, 2015
1 parent 45f901c commit 49d2936
Show file tree
Hide file tree
Showing 25 changed files with 189 additions and 35 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ if(USE_OPENGL)
# or even allow to use CUDA without any Gfx API...
if(USE_CUDA AND CUDA_FOUND)
add_subdirectory( FxLibCUDA )
list(APPEND NVFX_LIBS FxLibCUDA)
list(APPEND NVFX_LIBS FxLibCUDA) # FxLib refers to it when cmake allowed CUDA... so we need it for all :-(
endif()
# for now, Optix makes sense only for OpenGL, too
if(USE_OPTIX AND OPTIX_FOUND)
Expand Down
2 changes: 1 addition & 1 deletion FxLib/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ IShader* Container::createCUDAShader(const char *name, bool bGlobal)
pShdRep->addShader(p);
}
} else {
p = new_ShaderCUDA(name, this);
p = new_ShaderCUDA(name);
p->addUser(this);
if(!p)
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion FxLibCUDA/CUDAShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CUDAShader::~CUDAShader()
**
**
**/ /*************************************************************************/
CUDAShader::CUDAShader(const char * name, IContainer* pCont) : Shader(name, pCont)
CUDAShader::CUDAShader(const char * name) : Shader(name)
{
m_targetType = TCUDA;
}
Expand Down
10 changes: 5 additions & 5 deletions FxLibCUDA/FxCUDA.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace nvFX
public:
CstBufferCUDA(const char* name) : CstBuffer(name) {}
~CstBufferCUDA() {}
virtual CstBuffer* update(Pass *pass, int layerID, bool bBindProgram, bool bCreateIfNeeded, bool bCreateBufferIfNeeded)
virtual CstBuffer* update(Pass *pass, int layerID, bool bCreateIfNeeded, bool bCreateBufferIfNeeded)
{ /*TODO if needed*/return this; }
};
/*************************************************************************/ /**
Expand All @@ -146,9 +146,9 @@ namespace nvFX
virtual ~UniformCUDA();
UniformCUDA(const char* name = NULL, const char* groupname = NULL, const char* semantic = NULL);

Uniform* updateTextures(ShadowedData* data, Pass *pass, int layerID, bool bBindProgram, bool bCreateIfNeeded);
virtual Uniform* update(ShadowedData* pData, Pass *pass, int ppID, bool bBindProgram, bool bCreateIfNeeded);
virtual Uniform* updateForTarget(ShadowedData* pData, STarget &t, bool bBindProgram = false);
Uniform* updateTextures(ShadowedData* data, Pass *pass, int layerID, bool bCreateIfNeeded);
virtual Uniform* update(ShadowedData* pData, Pass *pass, int ppID, bool bCreateIfNeeded);
virtual Uniform* updateForTarget(ShadowedData* pData, int target);
};

/*************************************************************************/ /**
Expand All @@ -157,7 +157,7 @@ namespace nvFX
class CUDAShader : public Shader
{
public:
CUDAShader(const char *name = NULL, IContainer* pCont = NULL);
CUDAShader(const char *name = NULL);
~CUDAShader();

virtual void cleanupShader() {}
Expand Down
4 changes: 2 additions & 2 deletions FxLibCUDA/FxLibCUDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ void delete_ProgramCUDA(IProgram *pProg)
delete p;
}

Shader* new_ShaderCUDA(const char* name, Container *pCont)
Shader* new_ShaderCUDA(const char* name)
{
return new CUDAShader(name, pCont);
return new CUDAShader(name);
}
void delete_ShaderCUDA(IShader *pShd)
{
Expand Down
12 changes: 7 additions & 5 deletions FxLibCUDA/UniformCUDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ UniformCUDA::~UniformCUDA()
}
}

Uniform* UniformCUDA::update(ShadowedData* data, Pass *pass, int layerID, bool bBindProgram, bool bCreateIfNeeded)
Uniform* UniformCUDA::update(ShadowedData* data, Pass *pass, int layerID, bool bCreateIfNeeded)
{
CUresult res;
CUarray cuArray = NULL;
Expand All @@ -74,13 +74,13 @@ Uniform* UniformCUDA::update(ShadowedData* data, Pass *pass, int layerID, bool
case IUniform::TTexture3D:
case IUniform::TTextureCube:
//if(data && data->tex.pRes)
return updateTextures(data, pass, layerID, bBindProgram, bCreateIfNeeded);
return updateTextures(data, pass, layerID, bCreateIfNeeded);
break;
// TODO whenever necessary
}
return this;
}
Uniform* UniformCUDA::updateTextures(ShadowedData* data, Pass *pass, int layerID, bool bBindProgram, bool bCreateIfNeeded)
Uniform* UniformCUDA::updateTextures(ShadowedData* data, Pass *pass, int layerID, bool bCreateIfNeeded)
{
CUresult res;
CUarray cuArray = NULL;
Expand Down Expand Up @@ -156,17 +156,19 @@ Uniform* UniformCUDA::updateTextures(ShadowedData* data, Pass *pass, int layerI
if(data && data->tex.pRes)
static_cast<ResourceCUDA*>(data->tex.pRes)->setupAsCUDATexture();
// update again now we found it
return UniformCUDA::updateForTarget(data, t);
return UniformCUDA::updateForTarget(data, i);
}
}
}
return this;
}

Uniform* UniformCUDA::updateForTarget(ShadowedData* data, STarget &t, bool bBindProgram)
Uniform* UniformCUDA::updateForTarget(ShadowedData* data, int target)
{
CUresult res;
CUarray cuArray = NULL;
UniformCUDA::STarget &t = m_targets[target];
m_activeTarget = target;
if(data && data->tex.pRes)
{
cuArray = static_cast<ResourceCUDA*>(data->tex.pRes)->mapResource();
Expand Down
146 changes: 146 additions & 0 deletions cmake/FindCuda.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Try to find OptiX project dll and include file
#
unset(CUDA_DLL CACHE)
unset(CUDA_ROOT_DIR CACHE)
unset(CUDA_LIB CACHE)
unset(CUDA_VERSION CACHE)
unset(CUDA_FOUND CACHE)
unset(RESULT CACHE)

if ( NOT DEFINED ${CUDA_LOCATION} )
SET(CUDA_LOCATION "$ENV{CUDA_PATH}" CACHE PATH "path")
endif()

find_path( CUDA_ROOT_DIR include/cuda.h
${CUDA_LOCATION}
)

macro(_find_files targetVar incDir dllName dllName64 folder result)
unset ( result )
unset ( fileList )
if(ARCH STREQUAL "x86")
file(GLOB fileList "${${incDir}}/../${folder}${dllName}")
list(LENGTH fileList NUMLIST)
if(NUMLIST EQUAL 0)
file(GLOB fileList "${${incDir}}/${folder}${dllName}")
endif()
else()
file(GLOB fileList "${${incDir}}/../${folder}${dllName64}")
list(LENGTH fileList NUMLIST)
if(NUMLIST EQUAL 0)
file(GLOB fileList "${${incDir}}/${folder}${dllName64}")
endif()
endif()
list(LENGTH fileList NUMLIST)
if(NUMLIST EQUAL 0)
set (${result} "NOTFOUND" )
else()
set (${result} "OK" )
endif()
list(APPEND ${targetVar} ${fileList} )

endmacro()

if(CUDA_ROOT_DIR)
#---------Retrieve VERSION
set( CUDA_VERSION_MAX "0.0" )
string( REGEX REPLACE "(^.*)/[^/]*$" "\\1" CUDA_GENERAL_DIR ${CUDA_ROOT_DIR} )
file( GLOB CUDA_VERSION_PATHS ${CUDA_GENERAL_DIR}/* )
foreach( CUDA_VERSION_PATH ${CUDA_VERSION_PATHS} )
if( IS_DIRECTORY ${CUDA_VERSION_PATH} )
string( REGEX REPLACE "^.*/v([^/]*)$" "\\1" CUDA_VERSION_CURRENT ${CUDA_VERSION_PATH} )
if( CUDA_VERSION_CURRENT MATCHES "[0-9]+[.]+[0-9]+" )
if( ${CUDA_VERSION_MAX} VERSION_LESS ${CUDA_VERSION_CURRENT} )
set( CUDA_VERSION_MAX ${CUDA_VERSION_CURRENT} )
endif()
endif()
endif()
endforeach()

SET(CUDA_VERSION ${CUDA_VERSION_MAX} CACHE STRING "string")
string( REGEX REPLACE "(^[^.]*)[.]+.*$" "\\1" CUDA_VERSION_MAJOR ${CUDA_VERSION} )
string( REGEX REPLACE "^[^.]*[.]+(.*$)" "\\1" CUDA_VERSION_MINOR ${CUDA_VERSION} )

if( CUDA_VERSION_MAX STREQUAL "0.0" )
message( "CUDA directory is not valid" )
else()
set( CUDA_ROOT_DIR ${CUDA_GENERAL_DIR}/v${CUDA_VERSION} )
set( CUDA_NVCC_EXECUTABLE ${CUDA_ROOT_DIR}/bin/nvcc )
endif()


#-------- Locate DLLS
_find_files( CUDA_DLL CUDA_ROOT_DIR "cudart32_${CUDA_VERSION_MAJOR}${CUDA_VERSION_MINOR}.dll" "cudart64_${CUDA_VERSION_MAJOR}${CUDA_VERSION_MINOR}.dll" "bin/" RESULT)
if ( ${RESULT} STREQUAL "NOTFOUND" )
message( "CUDA is required for this sample (${PROJECT_NAME})." )
message( "CUDA binaries have not been found. Please set the CUDA_LOCATION variable above to the root location of the desired CUDA version." )
endif()

#-------- Locate LIBS
_find_files( CUDA_LIB CUDA_ROOT_DIR "win32/cudart.lib" "x64/cudart.lib" "lib/" RESULT)
_find_files( CUDA_LIB CUDA_ROOT_DIR "win32/cublas.lib" "x64/cublas.lib" "lib/" RESULT)
_find_files( CUDA_LIB CUDA_ROOT_DIR "win32/cuda.lib" "x64/cuda.lib" "lib/" RESULT)
_find_files( CUDA_LIB CUDA_ROOT_DIR "win32/OpenCL.lib" "x64/OpenCL.lib" "lib/" RESULT)

#-------- Locate HEADERS
_find_files( CUDA_HEADERS CUDA_ROOT_DIR "cuda.h" "cuda.h" "include/" RESULT)

if(CUDA_DLL)
set( CUDA_FOUND "YES" )
endif(CUDA_DLL)
else(CUDA_ROOT_DIR)

message(INFO "CUDA not found. Please set the CUDA_LOCATION variable above to the root location of CUDA 5.5.")

endif(CUDA_ROOT_DIR)

include(FindPackageHandleStandardArgs)

SET(CUDA_DLL ${CUDA_DLL} CACHE PATH "path")
SET(CUDA_LIB ${CUDA_LIB} CACHE PATH "path")
SET(CUDA_INCLUDE_DIR "${CUDA_ROOT_DIR}/include")

find_package_handle_standard_args(CUDA DEFAULT_MSG
CUDA_INCLUDE_DIR
CUDA_DLL
)

# Generate PTX files
# NVCUDA_COMPILE_PTX( SOURCES file1.cu file2.cu TARGET_PATH <path where ptxs should be stored> GENERATED_FILES ptx_sources NVCC_OPTIONS -arch=sm_20)
# Generates ptx files for the given source files. ptx_sources will contain the list of generated files.
FUNCTION(NVCUDA_COMPILE_PTX)
set(options "")
set(oneValueArgs TARGET_PATH GENERATED_FILES)
set(multiValueArgs NVCC_OPTIONS SOURCES)
CMAKE_PARSE_ARGUMENTS(NVCUDA_COMPILE_PTX "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

# Match the bitness of the ptx to the bitness of the application
set( MACHINE "--machine=32" )
if( CMAKE_SIZEOF_VOID_P EQUAL 8)
set( MACHINE "--machine=64" )
endif()

# Custom build rule to generate ptx files from cuda files
FOREACH( input ${NVCUDA_COMPILE_PTX_SOURCES} )
get_filename_component( input_we ${input} NAME_WE )

# generate the *.ptx files inside "ptx" folder inside the executable's output directory.
set( output "${CMAKE_CURRENT_SOURCE_DIR}/${input_we}.ptx" )

LIST( APPEND PTX_FILES ${output} )

message("${CUDA_NVCC_EXECUTABLE} ${MACHINE} --ptx ${NVCUDA_COMPILE_PTX_NVCC_OPTIONS} ${input} -o ${output} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}")

add_custom_command(
OUTPUT ${output}
DEPENDS ${input}
COMMAND ${CUDA_NVCC_EXECUTABLE} ${MACHINE} --ptx ${NVCUDA_COMPILE_PTX_NVCC_OPTIONS} ${input} -o ${output} WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMAND ${CMAKE_COMMAND} -E echo ${NVCUDA_COMPILE_PTX_TARGET_PATH}
COMMAND ${CMAKE_COMMAND} -E copy ${output} "${NVCUDA_COMPILE_PTX_TARGET_PATH}/${input_we}.ptx"
)
ENDFOREACH( )

set(${NVCUDA_COMPILE_PTX_GENERATED_FILES} ${PTX_FILES} PARENT_SCOPE)
ENDFUNCTION()

mark_as_advanced( CUDA_FOUND )
2 changes: 1 addition & 1 deletion nvFxcc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(nvFxcc)
set(PLATFORM_LIBRARIES
${NVFX_LIBS}
${OPENGL_LIBRARY}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)

Expand Down
2 changes: 1 addition & 1 deletion samples/D3DSkinnedModelEffect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(PLATFORM_LIBRARIES
${NVFX_LIBS}
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${CUDA_LIBRARIES}
${CUDA_LIB}
${ZLIB_LIBRARIES}
${NSIGHT_LIB}
)
Expand Down
2 changes: 1 addition & 1 deletion samples/OpenGLFluitSimEffect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${ZLIB_LIBRARIES}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)
include_directories(
Expand Down
2 changes: 1 addition & 1 deletion samples/OpenGLLigthingEffect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${ZLIB_LIBRARIES}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)
include_directories(
Expand Down
2 changes: 1 addition & 1 deletion samples/OpenGLSimpleEffect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${ZLIB_LIBRARIES}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)
include_directories(
Expand Down
2 changes: 1 addition & 1 deletion samples/OpenGLSimpleEffect_PNTriangles/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${ZLIB_LIBRARIES}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)
include_directories(
Expand Down
2 changes: 1 addition & 1 deletion samples/OpenGLSimpleEffect_Subroutine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${ZLIB_LIBRARIES}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)
include_directories(
Expand Down
2 changes: 1 addition & 1 deletion samples/OpenGLSimpleEffect_skinning/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${ZLIB_LIBRARIES}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)
include_directories(
Expand Down
2 changes: 1 addition & 1 deletion samples/OpenGLSimpleSceneEffect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${ZLIB_LIBRARIES}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)
include_directories(
Expand Down
2 changes: 1 addition & 1 deletion samples/OpenGLSkinnedModelEffect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(PLATFORM_LIBRARIES
${NVFX_LIBS}
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${CUDA_LIBRARIES}
${CUDA_LIB}
${ZLIB_LIBRARIES}
${NSIGHT_LIB}
)
Expand Down
2 changes: 1 addition & 1 deletion samples/OpenGLUniformUpdateExample/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(PLATFORM_LIBRARIES
${NVFX_LIBS}
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)
include_directories(
Expand Down
3 changes: 2 additions & 1 deletion samples/OpenGL_CUDAConvolution/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${ZLIB_LIBRARIES}
${CUDA_LIBRARIES}
${CUDA_LIB}
${NSIGHT_LIB}
)
include_directories(
Expand All @@ -16,6 +16,7 @@ include_directories(
${NV_MATH_DIR}
${BK3D_DIR}
${NV_IMAGE_DIR}
${CUDA_INCLUDE_DIR}
)
set(SOURCE_FILES
OpenGL_CUDAConvolution.cpp
Expand Down
Loading

0 comments on commit 49d2936

Please sign in to comment.