目录

Ubuntu上OptiX8 简单尝试

目录

CUDA 的安装参考之前的文章
需要安装 OptiX 8
这是Reference.pdf

新建一个 CMake 项目,目录结构是这样的

1
2
3
4
5
6
optix_example/
├── CMakeLists.txt
├── src
    └── main.cpp
└── cmake
    └── FindOptiX80.cmake

CMakeLists.txt文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cmake_minimum_required(VERSION 3.10)

project(OptiXExample)

set(CMAKE_CXX_STANDARD 17)

# Find OptiX package
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

find_package(OptiX80)

# Add CUDA

find_package(CUDAToolkit 12.0 REQUIRED)

# Include directories
include_directories(${OPTIX80_INCLUDE_DIR} ${CUDAToolkit_INCLUDE_DIRS})

# Source files
set(SOURCES src/main.cpp)

# Executable
add_executable(OptiXExample ${SOURCES})

# Link libraries
target_link_libraries(OptiXExample CUDA::cudart)

FindOptix80.cmake文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Looks for the environment variable:

# OPTIX80_PATH

# Sets the variables :

# OPTIX80_INCLUDE_DIR

# OptiX80_FOUND

set(OPTIX80_PATH $ENV{OPTIX80_PATH})

if("${OPTIX80_PATH}" STREQUAL "")
    if(WIN32)
        # Try finding it inside the default installation directory under Windows first.
        set(OPTIX80_PATH "C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0")
    else()
        # Adjust this if the OptiX SDK 8.0.0 installation is in a different location.
        set(OPTIX80_PATH "~/NVIDIA-OptiX-SDK-8.0.0-linux64")
    endif()
endif()

find_path(OPTIX80_INCLUDE_DIR optix_host.h ${OPTIX80_PATH}/include)

# message("OPTIX80_INCLUDE_DIR = " "${OPTIX80_INCLUDE_DIR}")
include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(OptiX80 DEFAULT_MSG OPTIX80_INCLUDE_DIR)

mark_as_advanced(OPTIX80_INCLUDE_DIR)

# message("OptiX80_FOUND = " "${OptiX80_FOUND}")

上面的内容是参考 Github 上OptiX_Apps,它读取了OPTIX80_PATH 环境变量,代表 Optix 在你机器上的位置,例如我的是/home/rabbit/NVIDIA-OptiX-SDK-8.0.0-linux64-x86_64

main.cpp 文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <optix.h>
#include <optix_stubs.h>
#include <optix_function_table_definition.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <stdexcept>
#include <cuda.h>
#include <cuda_runtime_api.h>

#define OPTIX_CHECK(call)                                                                             \
    {                                                                                                 \
        OptixResult res = call;                                                                       \
        if (res != OPTIX_SUCCESS)                                                                     \
        {                                                                                             \
            fprintf(stderr, "Optix call (%s) failed with code %d (line %d)\n", #call, res, __LINE__); \
            exit(2);                                                                                  \
        }                                                                                             \
    }

void initOptix()
{
    // -------------------------------------------------------
    // check for available optix7 capable devices
    // -------------------------------------------------------
    cudaFree(0);
    int numDevices;
    cudaGetDeviceCount(&numDevices);
    if (numDevices == 0)
        throw std::runtime_error("#osc: no CUDA capable devices found!");
    std::cout << "#osc: found " << numDevices << " CUDA devices" << std::endl;

    // -------------------------------------------------------
    // initialize optix
    // -------------------------------------------------------
    OPTIX_CHECK(optixInit());
}

int main()
{
    // Initialize CUDA
    cudaFree(0);

    try
    {
        std::cout << "#osc: initializing optix..." << std::endl;
        initOptix();

        std::cout
            << "#osc: successfully initialized optix... yay!"
            << std::endl;

        // for this simple hello-world example, don't do anything else
        // ...
        std::cout << "#osc: done. clean exit." << std::endl;
    }
    catch (std::runtime_error &e)
    {
        std::cout << "FATAL ERROR: " << e.what()
                  << std::endl;
        exit(1);
    }
    return 0;
}

最后编译,运行输出

1
2
3
4
5
$ ./OptiXExample
#osc: initializing optix...
#osc: found 1 CUDA devices
#osc: successfully initialized optix... yay!
#osc: done. clean exit.