From e12c08e8d170b7ca40f204a5b0423c23a9fbc2c1 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 17 Apr 2019 06:17:24 +0200 Subject: ClangFormat: apply to source, most of intern Apply clang format as proposed in T53211. For details on usage and instructions for migrating branches without conflicts, see: https://wiki.blender.org/wiki/Tools/ClangFormat --- intern/cycles/app/CMakeLists.txt | 202 +++--- intern/cycles/app/cycles_cubin_cc.cpp | 478 ++++++++------- intern/cycles/app/cycles_server.cpp | 158 ++--- intern/cycles/app/cycles_standalone.cpp | 794 +++++++++++++----------- intern/cycles/app/cycles_xml.cpp | 1012 ++++++++++++++++--------------- intern/cycles/app/cycles_xml.h | 2 +- 6 files changed, 1362 insertions(+), 1284 deletions(-) (limited to 'intern/cycles/app') diff --git a/intern/cycles/app/CMakeLists.txt b/intern/cycles/app/CMakeLists.txt index 2d7db860b09..36e3e179be5 100644 --- a/intern/cycles/app/CMakeLists.txt +++ b/intern/cycles/app/CMakeLists.txt @@ -1,6 +1,6 @@ set(INC - .. + .. ) set(INC_SYS ) @@ -8,46 +8,46 @@ set(INC_SYS # NOTE: LIBRARIES contains all the libraries which are common # across release and debug build types, stored in a linking order. set(LIBRARIES - cycles_device - cycles_kernel - cycles_render - cycles_bvh - cycles_subd - cycles_graph - cycles_util - ${BLENDER_GL_LIBRARIES} - ${CYCLES_APP_GLEW_LIBRARY} - ${PNG_LIBRARIES} - ${JPEG_LIBRARIES} - ${ZLIB_LIBRARIES} - ${TIFF_LIBRARY} - ${PTHREADS_LIBRARIES} - extern_clew + cycles_device + cycles_kernel + cycles_render + cycles_bvh + cycles_subd + cycles_graph + cycles_util + ${BLENDER_GL_LIBRARIES} + ${CYCLES_APP_GLEW_LIBRARY} + ${PNG_LIBRARIES} + ${JPEG_LIBRARIES} + ${ZLIB_LIBRARIES} + ${TIFF_LIBRARY} + ${PTHREADS_LIBRARIES} + extern_clew ) if(WITH_CUDA_DYNLOAD) - list(APPEND LIBRARIES extern_cuew) + list(APPEND LIBRARIES extern_cuew) else() - list(APPEND LIBRARIES ${CUDA_CUDA_LIBRARY}) + list(APPEND LIBRARIES ${CUDA_CUDA_LIBRARY}) endif() if(WITH_CYCLES_OSL) - list(APPEND LIBRARIES cycles_kernel_osl) + list(APPEND LIBRARIES cycles_kernel_osl) endif() if(NOT CYCLES_STANDALONE_REPOSITORY) - list(APPEND LIBRARIES bf_intern_glew_mx bf_intern_guardedalloc bf_intern_numaapi) + list(APPEND LIBRARIES bf_intern_glew_mx bf_intern_guardedalloc bf_intern_numaapi) endif() if(WITH_CYCLES_LOGGING) - list(APPEND LIBRARIES - ${GLOG_LIBRARIES} - ${GFLAGS_LIBRARIES} - ) + list(APPEND LIBRARIES + ${GLOG_LIBRARIES} + ${GFLAGS_LIBRARIES} + ) endif() if(WITH_CYCLES_STANDALONE AND WITH_CYCLES_STANDALONE_GUI) - list(APPEND LIBRARIES ${GLUT_LIBRARIES}) + list(APPEND LIBRARIES ${GLUT_LIBRARIES}) endif() # Common configuration. @@ -62,7 +62,7 @@ link_directories(${OPENIMAGEIO_LIBPATH} ${OPENJPEG_LIBPATH}) if(WITH_OPENCOLORIO) - link_directories(${OPENCOLORIO_LIBPATH}) + link_directories(${OPENCOLORIO_LIBPATH}) endif() add_definitions(${GL_DEFINITIONS}) @@ -78,90 +78,90 @@ include_directories(SYSTEM ${INC_SYS}) # # TODO(sergey): Think of a better place for this? macro(cycles_target_link_libraries target) - target_link_libraries(${target} ${LIBRARIES}) - if(WITH_CYCLES_OSL) - target_link_libraries(${target} ${OSL_LIBRARIES} ${LLVM_LIBRARIES}) - endif() - if(WITH_CYCLES_EMBREE) - target_link_libraries(${target} ${EMBREE_LIBRARIES}) - endif() - if(WITH_OPENSUBDIV) - target_link_libraries(${target} ${OPENSUBDIV_LIBRARIES}) - endif() - if(WITH_OPENCOLORIO) - target_link_libraries(${target} ${OPENCOLORIO_LIBRARIES}) - endif() - target_link_libraries( - ${target} - ${OPENIMAGEIO_LIBRARIES} - ${OPENEXR_LIBRARIES} - ${OPENJPEG_LIBRARIES} - ${PUGIXML_LIBRARIES} - ${BOOST_LIBRARIES} - ${CMAKE_DL_LIBS} - ${PLATFORM_LINKLIBS} - ) + target_link_libraries(${target} ${LIBRARIES}) + if(WITH_CYCLES_OSL) + target_link_libraries(${target} ${OSL_LIBRARIES} ${LLVM_LIBRARIES}) + endif() + if(WITH_CYCLES_EMBREE) + target_link_libraries(${target} ${EMBREE_LIBRARIES}) + endif() + if(WITH_OPENSUBDIV) + target_link_libraries(${target} ${OPENSUBDIV_LIBRARIES}) + endif() + if(WITH_OPENCOLORIO) + target_link_libraries(${target} ${OPENCOLORIO_LIBRARIES}) + endif() + target_link_libraries( + ${target} + ${OPENIMAGEIO_LIBRARIES} + ${OPENEXR_LIBRARIES} + ${OPENJPEG_LIBRARIES} + ${PUGIXML_LIBRARIES} + ${BOOST_LIBRARIES} + ${CMAKE_DL_LIBS} + ${PLATFORM_LINKLIBS} + ) endmacro() # Application build targets if(WITH_CYCLES_STANDALONE) - set(SRC - cycles_standalone.cpp - cycles_xml.cpp - cycles_xml.h - ) - add_executable(cycles ${SRC}) - cycles_target_link_libraries(cycles) - - if(UNIX AND NOT APPLE) - set_target_properties(cycles PROPERTIES INSTALL_RPATH $ORIGIN/lib) - endif() - unset(SRC) + set(SRC + cycles_standalone.cpp + cycles_xml.cpp + cycles_xml.h + ) + add_executable(cycles ${SRC}) + cycles_target_link_libraries(cycles) + + if(UNIX AND NOT APPLE) + set_target_properties(cycles PROPERTIES INSTALL_RPATH $ORIGIN/lib) + endif() + unset(SRC) endif() if(WITH_CYCLES_NETWORK) - set(SRC - cycles_server.cpp - ) - add_executable(cycles_server ${SRC}) - cycles_target_link_libraries(cycles_server) - - if(UNIX AND NOT APPLE) - set_target_properties(cycles_server PROPERTIES INSTALL_RPATH $ORIGIN/lib) - endif() - unset(SRC) + set(SRC + cycles_server.cpp + ) + add_executable(cycles_server ${SRC}) + cycles_target_link_libraries(cycles_server) + + if(UNIX AND NOT APPLE) + set_target_properties(cycles_server PROPERTIES INSTALL_RPATH $ORIGIN/lib) + endif() + unset(SRC) endif() if(WITH_CYCLES_CUBIN_COMPILER) - # 32 bit windows is special, nvrtc is not supported on x86, so even - # though we are building 32 bit blender a 64 bit cubin_cc will have - # to be build to compile the cubins. - if(MSVC AND NOT CMAKE_CL_64) - message("Building with CUDA not supported on 32 bit, skipped") - set(WITH_CYCLES_CUDA_BINARIES OFF CACHE BOOL "" FORCE) - else() - set(SRC - cycles_cubin_cc.cpp - ) - set(INC - ../../../extern/cuew/include - ) - add_executable(cycles_cubin_cc ${SRC}) - include_directories(${INC}) - target_link_libraries(cycles_cubin_cc - extern_cuew - ${OPENIMAGEIO_LIBRARIES} - ${OPENEXR_LIBRARIES} - ${OPENJPEG_LIBRARIES} - ${PUGIXML_LIBRARIES} - ${BOOST_LIBRARIES} - ${PLATFORM_LINKLIBS} - ) - if(NOT CYCLES_STANDALONE_REPOSITORY) - target_link_libraries(cycles_cubin_cc bf_intern_guardedalloc) - endif() - unset(SRC) - unset(INC) - endif() + # 32 bit windows is special, nvrtc is not supported on x86, so even + # though we are building 32 bit blender a 64 bit cubin_cc will have + # to be build to compile the cubins. + if(MSVC AND NOT CMAKE_CL_64) + message("Building with CUDA not supported on 32 bit, skipped") + set(WITH_CYCLES_CUDA_BINARIES OFF CACHE BOOL "" FORCE) + else() + set(SRC + cycles_cubin_cc.cpp + ) + set(INC + ../../../extern/cuew/include + ) + add_executable(cycles_cubin_cc ${SRC}) + include_directories(${INC}) + target_link_libraries(cycles_cubin_cc + extern_cuew + ${OPENIMAGEIO_LIBRARIES} + ${OPENEXR_LIBRARIES} + ${OPENJPEG_LIBRARIES} + ${PUGIXML_LIBRARIES} + ${BOOST_LIBRARIES} + ${PLATFORM_LINKLIBS} + ) + if(NOT CYCLES_STANDALONE_REPOSITORY) + target_link_libraries(cycles_cubin_cc bf_intern_guardedalloc) + endif() + unset(SRC) + unset(INC) + endif() endif() diff --git a/intern/cycles/app/cycles_cubin_cc.cpp b/intern/cycles/app/cycles_cubin_cc.cpp index e6eb0be0d91..774c18f4219 100644 --- a/intern/cycles/app/cycles_cubin_cc.cpp +++ b/intern/cycles/app/cycles_cubin_cc.cpp @@ -26,272 +26,286 @@ #include "cuew.h" #ifdef _MSC_VER -# include +# include #endif using std::string; using std::vector; namespace std { - template - std::string to_string(const T &n) { - std::ostringstream s; - s << n; - return s.str(); - } -} - -class CompilationSettings +template std::string to_string(const T &n) { -public: - CompilationSettings() - : target_arch(0), - bits(64), - verbose(false), - fast_math(false) - {} - - string cuda_toolkit_dir; - string input_file; - string output_file; - string ptx_file; - vector defines; - vector includes; - int target_arch; - int bits; - bool verbose; - bool fast_math; + std::ostringstream s; + s << n; + return s.str(); +} +} // namespace std + +class CompilationSettings { + public: + CompilationSettings() : target_arch(0), bits(64), verbose(false), fast_math(false) + { + } + + string cuda_toolkit_dir; + string input_file; + string output_file; + string ptx_file; + vector defines; + vector includes; + int target_arch; + int bits; + bool verbose; + bool fast_math; }; static bool compile_cuda(CompilationSettings &settings) { - const char* headers[] = {"stdlib.h" , "float.h", "math.h", "stdio.h"}; - const char* header_content[] = {"\n", "\n", "\n", "\n"}; - - printf("Building %s\n", settings.input_file.c_str()); - - string code; - if(!OIIO::Filesystem::read_text_file(settings.input_file, code)) { - fprintf(stderr, "Error: unable to read %s\n", settings.input_file.c_str()); - return false; - } - - vector options; - for(size_t i = 0; i < settings.includes.size(); i++) { - options.push_back("-I" + settings.includes[i]); - } - - for(size_t i = 0; i < settings.defines.size(); i++) { - options.push_back("-D" + settings.defines[i]); - } - options.push_back("-D__KERNEL_CUDA_VERSION__=" + std::to_string(cuewNvrtcVersion())); - options.push_back("-arch=compute_" + std::to_string(settings.target_arch)); - options.push_back("--device-as-default-execution-space"); - if(settings.fast_math) - options.push_back("--use_fast_math"); - - nvrtcProgram prog; - nvrtcResult result = nvrtcCreateProgram(&prog, - code.c_str(), // buffer - NULL, // name - sizeof(headers) / sizeof(void*), // numHeaders - header_content, // headers - headers); // includeNames - - if(result != NVRTC_SUCCESS) { - fprintf(stderr, "Error: nvrtcCreateProgram failed (%d)\n\n", (int)result); - return false; - } - - /* Tranfer options to a classic C array. */ - vector opts(options.size()); - for(size_t i = 0; i < options.size(); i++) { - opts[i] = options[i].c_str(); - } - - result = nvrtcCompileProgram(prog, options.size(), &opts[0]); - - if(result != NVRTC_SUCCESS) { - fprintf(stderr, "Error: nvrtcCompileProgram failed (%d)\n\n", (int)result); - - size_t log_size; - nvrtcGetProgramLogSize(prog, &log_size); - - vector log(log_size); - nvrtcGetProgramLog(prog, &log[0]); - fprintf(stderr, "%s\n", &log[0]); - - return false; - } - - /* Retrieve the ptx code. */ - size_t ptx_size; - result = nvrtcGetPTXSize(prog, &ptx_size); - if(result != NVRTC_SUCCESS) { - fprintf(stderr, "Error: nvrtcGetPTXSize failed (%d)\n\n", (int)result); - return false; - } - - vector ptx_code(ptx_size); - result = nvrtcGetPTX(prog, &ptx_code[0]); - if(result != NVRTC_SUCCESS) { - fprintf(stderr, "Error: nvrtcGetPTX failed (%d)\n\n", (int)result); - return false; - } - - /* Write a file in the temp folder with the ptx code. */ - settings.ptx_file = OIIO::Filesystem::temp_directory_path() + "/" + OIIO::Filesystem::unique_path(); - FILE * f= fopen(settings.ptx_file.c_str(), "wb"); - fwrite(&ptx_code[0], 1, ptx_size, f); - fclose(f); - - return true; + const char *headers[] = {"stdlib.h", "float.h", "math.h", "stdio.h"}; + const char *header_content[] = {"\n", "\n", "\n", "\n"}; + + printf("Building %s\n", settings.input_file.c_str()); + + string code; + if (!OIIO::Filesystem::read_text_file(settings.input_file, code)) { + fprintf(stderr, "Error: unable to read %s\n", settings.input_file.c_str()); + return false; + } + + vector options; + for (size_t i = 0; i < settings.includes.size(); i++) { + options.push_back("-I" + settings.includes[i]); + } + + for (size_t i = 0; i < settings.defines.size(); i++) { + options.push_back("-D" + settings.defines[i]); + } + options.push_back("-D__KERNEL_CUDA_VERSION__=" + std::to_string(cuewNvrtcVersion())); + options.push_back("-arch=compute_" + std::to_string(settings.target_arch)); + options.push_back("--device-as-default-execution-space"); + if (settings.fast_math) + options.push_back("--use_fast_math"); + + nvrtcProgram prog; + nvrtcResult result = nvrtcCreateProgram(&prog, + code.c_str(), // buffer + NULL, // name + sizeof(headers) / sizeof(void *), // numHeaders + header_content, // headers + headers); // includeNames + + if (result != NVRTC_SUCCESS) { + fprintf(stderr, "Error: nvrtcCreateProgram failed (%d)\n\n", (int)result); + return false; + } + + /* Tranfer options to a classic C array. */ + vector opts(options.size()); + for (size_t i = 0; i < options.size(); i++) { + opts[i] = options[i].c_str(); + } + + result = nvrtcCompileProgram(prog, options.size(), &opts[0]); + + if (result != NVRTC_SUCCESS) { + fprintf(stderr, "Error: nvrtcCompileProgram failed (%d)\n\n", (int)result); + + size_t log_size; + nvrtcGetProgramLogSize(prog, &log_size); + + vector log(log_size); + nvrtcGetProgramLog(prog, &log[0]); + fprintf(stderr, "%s\n", &log[0]); + + return false; + } + + /* Retrieve the ptx code. */ + size_t ptx_size; + result = nvrtcGetPTXSize(prog, &ptx_size); + if (result != NVRTC_SUCCESS) { + fprintf(stderr, "Error: nvrtcGetPTXSize failed (%d)\n\n", (int)result); + return false; + } + + vector ptx_code(ptx_size); + result = nvrtcGetPTX(prog, &ptx_code[0]); + if (result != NVRTC_SUCCESS) { + fprintf(stderr, "Error: nvrtcGetPTX failed (%d)\n\n", (int)result); + return false; + } + + /* Write a file in the temp folder with the ptx code. */ + settings.ptx_file = OIIO::Filesystem::temp_directory_path() + "/" + + OIIO::Filesystem::unique_path(); + FILE *f = fopen(settings.ptx_file.c_str(), "wb"); + fwrite(&ptx_code[0], 1, ptx_size, f); + fclose(f); + + return true; } static bool link_ptxas(CompilationSettings &settings) { - string cudapath = ""; - if(settings.cuda_toolkit_dir.size()) - cudapath = settings.cuda_toolkit_dir + "/bin/"; - - string ptx = "\"" +cudapath + "ptxas\" " + settings.ptx_file + - " -o " + settings.output_file + - " --gpu-name sm_" + std::to_string(settings.target_arch) + - " -m" + std::to_string(settings.bits); - - if(settings.verbose) { - ptx += " --verbose"; - printf("%s\n", ptx.c_str()); - } - - int pxresult = system(ptx.c_str()); - if(pxresult) { - fprintf(stderr, "Error: ptxas failed (%d)\n\n", pxresult); - return false; - } - - if(!OIIO::Filesystem::remove(settings.ptx_file)) { - fprintf(stderr, "Error: removing %s\n\n", settings.ptx_file.c_str()); - } - - return true; + string cudapath = ""; + if (settings.cuda_toolkit_dir.size()) + cudapath = settings.cuda_toolkit_dir + "/bin/"; + + string ptx = "\"" + cudapath + "ptxas\" " + settings.ptx_file + " -o " + settings.output_file + + " --gpu-name sm_" + std::to_string(settings.target_arch) + " -m" + + std::to_string(settings.bits); + + if (settings.verbose) { + ptx += " --verbose"; + printf("%s\n", ptx.c_str()); + } + + int pxresult = system(ptx.c_str()); + if (pxresult) { + fprintf(stderr, "Error: ptxas failed (%d)\n\n", pxresult); + return false; + } + + if (!OIIO::Filesystem::remove(settings.ptx_file)) { + fprintf(stderr, "Error: removing %s\n\n", settings.ptx_file.c_str()); + } + + return true; } static bool init(CompilationSettings &settings) { #ifdef _MSC_VER - if(settings.cuda_toolkit_dir.size()) { - SetDllDirectory((settings.cuda_toolkit_dir + "/bin").c_str()); - } + if (settings.cuda_toolkit_dir.size()) { + SetDllDirectory((settings.cuda_toolkit_dir + "/bin").c_str()); + } #else - (void)settings; + (void)settings; #endif - int cuewresult = cuewInit(CUEW_INIT_NVRTC); - if(cuewresult != CUEW_SUCCESS) { - fprintf(stderr, "Error: cuew init fialed (0x%d)\n\n", cuewresult); - return false; - } - - if(cuewNvrtcVersion() < 80) { - fprintf(stderr, "Error: only cuda 8 and higher is supported, %d\n\n", cuewCompilerVersion()); - return false; - } - - if(!nvrtcCreateProgram) { - fprintf(stderr, "Error: nvrtcCreateProgram not resolved\n"); - return false; - } - - if(!nvrtcCompileProgram) { - fprintf(stderr, "Error: nvrtcCompileProgram not resolved\n"); - return false; - } - - if(!nvrtcGetProgramLogSize) { - fprintf(stderr, "Error: nvrtcGetProgramLogSize not resolved\n"); - return false; - } - - if(!nvrtcGetProgramLog) { - fprintf(stderr, "Error: nvrtcGetProgramLog not resolved\n"); - return false; - } - - if(!nvrtcGetPTXSize) { - fprintf(stderr, "Error: nvrtcGetPTXSize not resolved\n"); - return false; - } - - if(!nvrtcGetPTX) { - fprintf(stderr, "Error: nvrtcGetPTX not resolved\n"); - return false; - } - - return true; + int cuewresult = cuewInit(CUEW_INIT_NVRTC); + if (cuewresult != CUEW_SUCCESS) { + fprintf(stderr, "Error: cuew init fialed (0x%d)\n\n", cuewresult); + return false; + } + + if (cuewNvrtcVersion() < 80) { + fprintf(stderr, "Error: only cuda 8 and higher is supported, %d\n\n", cuewCompilerVersion()); + return false; + } + + if (!nvrtcCreateProgram) { + fprintf(stderr, "Error: nvrtcCreateProgram not resolved\n"); + return false; + } + + if (!nvrtcCompileProgram) { + fprintf(stderr, "Error: nvrtcCompileProgram not resolved\n"); + return false; + } + + if (!nvrtcGetProgramLogSize) { + fprintf(stderr, "Error: nvrtcGetProgramLogSize not resolved\n"); + return false; + } + + if (!nvrtcGetProgramLog) { + fprintf(stderr, "Error: nvrtcGetProgramLog not resolved\n"); + return false; + } + + if (!nvrtcGetPTXSize) { + fprintf(stderr, "Error: nvrtcGetPTXSize not resolved\n"); + return false; + } + + if (!nvrtcGetPTX) { + fprintf(stderr, "Error: nvrtcGetPTX not resolved\n"); + return false; + } + + return true; } static bool parse_parameters(int argc, const char **argv, CompilationSettings &settings) { - OIIO::ArgParse ap; - ap.options("Usage: cycles_cubin_cc [options]", - "-target %d", &settings.target_arch, "target shader model", - "-m %d", &settings.bits, "Cuda architecture bits", - "-i %s", &settings.input_file, "Input source filename", - "-o %s", &settings.output_file, "Output cubin filename", - "-I %L", &settings.includes, "Add additional includepath", - "-D %L", &settings.defines, "Add additional defines", - "-v", &settings.verbose, "Use verbose logging", - "--use_fast_math", &settings.fast_math, "Use fast math", - "-cuda-toolkit-dir %s", &settings.cuda_toolkit_dir, "path to the cuda toolkit binary directory", - NULL); - - if(ap.parse(argc, argv) < 0) { - fprintf(stderr, "%s\n", ap.geterror().c_str()); - ap.usage(); - return false; - } - - if(!settings.output_file.size()) { - fprintf(stderr, "Error: Output file not set(-o), required\n\n"); - return false; - } - - if(!settings.input_file.size()) { - fprintf(stderr, "Error: Input file not set(-i, required\n\n"); - return false; - } - - if(!settings.target_arch) { - fprintf(stderr, "Error: target shader model not set (-target), required\n\n"); - return false; - } - - return true; + OIIO::ArgParse ap; + ap.options("Usage: cycles_cubin_cc [options]", + "-target %d", + &settings.target_arch, + "target shader model", + "-m %d", + &settings.bits, + "Cuda architecture bits", + "-i %s", + &settings.input_file, + "Input source filename", + "-o %s", + &settings.output_file, + "Output cubin filename", + "-I %L", + &settings.includes, + "Add additional includepath", + "-D %L", + &settings.defines, + "Add additional defines", + "-v", + &settings.verbose, + "Use verbose logging", + "--use_fast_math", + &settings.fast_math, + "Use fast math", + "-cuda-toolkit-dir %s", + &settings.cuda_toolkit_dir, + "path to the cuda toolkit binary directory", + NULL); + + if (ap.parse(argc, argv) < 0) { + fprintf(stderr, "%s\n", ap.geterror().c_str()); + ap.usage(); + return false; + } + + if (!settings.output_file.size()) { + fprintf(stderr, "Error: Output file not set(-o), required\n\n"); + return false; + } + + if (!settings.input_file.size()) { + fprintf(stderr, "Error: Input file not set(-i, required\n\n"); + return false; + } + + if (!settings.target_arch) { + fprintf(stderr, "Error: target shader model not set (-target), required\n\n"); + return false; + } + + return true; } int main(int argc, const char **argv) { - CompilationSettings settings; + CompilationSettings settings; - if(!parse_parameters(argc, argv, settings)) { - fprintf(stderr, "Error: invalid parameters, exiting\n"); - exit(EXIT_FAILURE); - } + if (!parse_parameters(argc, argv, settings)) { + fprintf(stderr, "Error: invalid parameters, exiting\n"); + exit(EXIT_FAILURE); + } - if(!init(settings)) { - fprintf(stderr, "Error: initialization error, exiting\n"); - exit(EXIT_FAILURE); - } + if (!init(settings)) { + fprintf(stderr, "Error: initialization error, exiting\n"); + exit(EXIT_FAILURE); + } - if(!compile_cuda(settings)) { - fprintf(stderr, "Error: compilation error, exiting\n"); - exit(EXIT_FAILURE); - } + if (!compile_cuda(settings)) { + fprintf(stderr, "Error: compilation error, exiting\n"); + exit(EXIT_FAILURE); + } - if(!link_ptxas(settings)) { - exit(EXIT_FAILURE); - } + if (!link_ptxas(settings)) { + exit(EXIT_FAILURE); + } - return 0; + return 0; } diff --git a/intern/cycles/app/cycles_server.cpp b/intern/cycles/app/cycles_server.cpp index e2166473aa3..c5a4c9b375b 100644 --- a/intern/cycles/app/cycles_server.cpp +++ b/intern/cycles/app/cycles_server.cpp @@ -30,85 +30,93 @@ using namespace ccl; int main(int argc, const char **argv) { - util_logging_init(argv[0]); - path_init(); + util_logging_init(argv[0]); + path_init(); + + /* device types */ + string devicelist = ""; + string devicename = "cpu"; + bool list = false, debug = false; + int threads = 0, verbosity = 1; + + vector &types = Device::available_types(); + + foreach (DeviceType type, types) { + if (devicelist != "") + devicelist += ", "; + + devicelist += Device::string_from_type(type); + } + + /* parse options */ + ArgParse ap; + + ap.options("Usage: cycles_server [options]", + "--device %s", + &devicename, + ("Devices to use: " + devicelist).c_str(), + "--list-devices", + &list, + "List information about all available devices", + "--threads %d", + &threads, + "Number of threads to use for CPU device", +#ifdef WITH_CYCLES_LOGGING + "--debug", + &debug, + "Enable debug logging", + "--verbose %d", + &verbosity, + "Set verbosity of the logger", +#endif + NULL); - /* device types */ - string devicelist = ""; - string devicename = "cpu"; - bool list = false, debug = false; - int threads = 0, verbosity = 1; + if (ap.parse(argc, argv) < 0) { + fprintf(stderr, "%s\n", ap.geterror().c_str()); + ap.usage(); + exit(EXIT_FAILURE); + } - vector& types = Device::available_types(); + if (debug) { + util_logging_start(); + util_logging_verbosity_set(verbosity); + } - foreach(DeviceType type, types) { - if(devicelist != "") - devicelist += ", "; + if (list) { + vector &devices = Device::available_devices(); - devicelist += Device::string_from_type(type); - } + printf("Devices:\n"); - /* parse options */ - ArgParse ap; + foreach (DeviceInfo &info, devices) { + printf(" %s%s\n", info.description.c_str(), (info.display_device) ? " (display)" : ""); + } - ap.options ("Usage: cycles_server [options]", - "--device %s", &devicename, ("Devices to use: " + devicelist).c_str(), - "--list-devices", &list, "List information about all available devices", - "--threads %d", &threads, "Number of threads to use for CPU device", -#ifdef WITH_CYCLES_LOGGING - "--debug", &debug, "Enable debug logging", - "--verbose %d", &verbosity, "Set verbosity of the logger", -#endif - NULL); - - if(ap.parse(argc, argv) < 0) { - fprintf(stderr, "%s\n", ap.geterror().c_str()); - ap.usage(); - exit(EXIT_FAILURE); - } - - if(debug) { - util_logging_start(); - util_logging_verbosity_set(verbosity); - } - - if(list) { - vector& devices = Device::available_devices(); - - printf("Devices:\n"); - - foreach(DeviceInfo& info, devices) { - printf(" %s%s\n", - info.description.c_str(), - (info.display_device)? " (display)": ""); - } - - exit(EXIT_SUCCESS); - } - - /* find matching device */ - DeviceType device_type = Device::type_from_string(devicename.c_str()); - vector& devices = Device::available_devices(); - DeviceInfo device_info; - - foreach(DeviceInfo& device, devices) { - if(device_type == device.type) { - device_info = device; - break; - } - } - - TaskScheduler::init(threads); - - while(1) { - Stats stats; - Device *device = Device::create(device_info, stats, true); - printf("Cycles Server with device: %s\n", device->info.description.c_str()); - device->server_run(); - delete device; - } - - TaskScheduler::exit(); - - return 0; + exit(EXIT_SUCCESS); + } + + /* find matching device */ + DeviceType device_type = Device::type_from_string(devicename.c_str()); + vector &devices = Device::available_devices(); + DeviceInfo device_info; + + foreach (DeviceInfo &device, devices) { + if (device_type == device.type) { + device_info = device; + break; + } + } + + TaskScheduler::init(threads); + + while (1) { + Stats stats; + Device *device = Device::create(device_info, stats, true); + printf("Cycles Server with device: %s\n", device->info.description.c_str()); + device->server_run(); + delete device; + } + + TaskScheduler::exit(); + + return 0; } diff --git a/intern/cycles/app/cycles_standalone.cpp b/intern/cycles/app/cycles_standalone.cpp index 9c899a38e7b..d2d112e8d7e 100644 --- a/intern/cycles/app/cycles_standalone.cpp +++ b/intern/cycles/app/cycles_standalone.cpp @@ -36,7 +36,7 @@ #include "util/util_version.h" #ifdef WITH_CYCLES_STANDALONE_GUI -#include "util/util_view.h" +# include "util/util_view.h" #endif #include "app/cycles_xml.h" @@ -44,447 +44,494 @@ CCL_NAMESPACE_BEGIN struct Options { - Session *session; - Scene *scene; - string filepath; - int width, height; - SceneParams scene_params; - SessionParams session_params; - bool quiet; - bool show_help, interactive, pause; - string output_path; + Session *session; + Scene *scene; + string filepath; + int width, height; + SceneParams scene_params; + SessionParams session_params; + bool quiet; + bool show_help, interactive, pause; + string output_path; } options; -static void session_print(const string& str) +static void session_print(const string &str) { - /* print with carriage return to overwrite previous */ - printf("\r%s", str.c_str()); + /* print with carriage return to overwrite previous */ + printf("\r%s", str.c_str()); - /* add spaces to overwrite longer previous print */ - static int maxlen = 0; - int len = str.size(); - maxlen = max(len, maxlen); + /* add spaces to overwrite longer previous print */ + static int maxlen = 0; + int len = str.size(); + maxlen = max(len, maxlen); - for(int i = len; i < maxlen; i++) - printf(" "); + for (int i = len; i < maxlen; i++) + printf(" "); - /* flush because we don't write an end of line */ - fflush(stdout); + /* flush because we don't write an end of line */ + fflush(stdout); } static void session_print_status() { - string status, substatus; + string status, substatus; - /* get status */ - float progress = options.session->progress.get_progress(); - options.session->progress.get_status(status, substatus); + /* get status */ + float progress = options.session->progress.get_progress(); + options.session->progress.get_status(status, substatus); - if(substatus != "") - status += ": " + substatus; + if (substatus != "") + status += ": " + substatus; - /* print status */ - status = string_printf("Progress %05.2f %s", (double) progress*100, status.c_str()); - session_print(status); + /* print status */ + status = string_printf("Progress %05.2f %s", (double)progress * 100, status.c_str()); + session_print(status); } static bool write_render(const uchar *pixels, int w, int h, int channels) { - string msg = string_printf("Writing image %s", options.output_path.c_str()); - session_print(msg); + string msg = string_printf("Writing image %s", options.output_path.c_str()); + session_print(msg); - unique_ptr out = unique_ptr(ImageOutput::create(options.output_path)); - if(!out) { - return false; - } + unique_ptr out = unique_ptr(ImageOutput::create(options.output_path)); + if (!out) { + return false; + } - ImageSpec spec(w, h, channels, TypeDesc::UINT8); - if(!out->open(options.output_path, spec)) { - return false; - } + ImageSpec spec(w, h, channels, TypeDesc::UINT8); + if (!out->open(options.output_path, spec)) { + return false; + } - /* conversion for different top/bottom convention */ - out->write_image(TypeDesc::UINT8, - pixels + (h - 1) * w * channels, - AutoStride, - -w * channels, - AutoStride); + /* conversion for different top/bottom convention */ + out->write_image( + TypeDesc::UINT8, pixels + (h - 1) * w * channels, AutoStride, -w * channels, AutoStride); - out->close(); + out->close(); - return true; + return true; } -static BufferParams& session_buffer_params() +static BufferParams &session_buffer_params() { - static BufferParams buffer_params; - buffer_params.width = options.width; - buffer_params.height = options.height; - buffer_params.full_width = options.width; - buffer_params.full_height = options.height; + static BufferParams buffer_params; + buffer_params.width = options.width; + buffer_params.height = options.height; + buffer_params.full_width = options.width; + buffer_params.full_height = options.height; - return buffer_params; + return buffer_params; } static void scene_init() { - options.scene = new Scene(options.scene_params, options.session->device); - - /* Read XML */ - xml_read_file(options.scene, options.filepath.c_str()); - - /* Camera width/height override? */ - if(!(options.width == 0 || options.height == 0)) { - options.scene->camera->width = options.width; - options.scene->camera->height = options.height; - } - else { - options.width = options.scene->camera->width; - options.height = options.scene->camera->height; - } - - /* Calculate Viewplane */ - options.scene->camera->compute_auto_viewplane(); + options.scene = new Scene(options.scene_params, options.session->device); + + /* Read XML */ + xml_read_file(options.scene, options.filepath.c_str()); + + /* Camera width/height override? */ + if (!(options.width == 0 || options.height == 0)) { + options.scene->camera->width = options.width; + options.scene->camera->height = options.height; + } + else { + options.width = options.scene->camera->width; + options.height = options.scene->camera->height; + } + + /* Calculate Viewplane */ + options.scene->camera->compute_auto_viewplane(); } static void session_init() { - options.session_params.write_render_cb = write_render; - options.session = new Session(options.session_params); + options.session_params.write_render_cb = write_render; + options.session = new Session(options.session_params); - if(options.session_params.background && !options.quiet) - options.session->progress.set_update_callback(function_bind(&session_print_status)); + if (options.session_params.background && !options.quiet) + options.session->progress.set_update_callback(function_bind(&session_print_status)); #ifdef WITH_CYCLES_STANDALONE_GUI - else - options.session->progress.set_update_callback(function_bind(&view_redraw)); + else + options.session->progress.set_update_callback(function_bind(&view_redraw)); #endif - /* load scene */ - scene_init(); - options.session->scene = options.scene; + /* load scene */ + scene_init(); + options.session->scene = options.scene; - options.session->reset(session_buffer_params(), options.session_params.samples); - options.session->start(); + options.session->reset(session_buffer_params(), options.session_params.samples); + options.session->start(); } static void session_exit() { - if(options.session) { - delete options.session; - options.session = NULL; - } - - if(options.session_params.background && !options.quiet) { - session_print("Finished Rendering."); - printf("\n"); - } + if (options.session) { + delete options.session; + options.session = NULL; + } + + if (options.session_params.background && !options.quiet) { + session_print("Finished Rendering."); + printf("\n"); + } } #ifdef WITH_CYCLES_STANDALONE_GUI -static void display_info(Progress& progress) +static void display_info(Progress &progress) { - static double latency = 0.0; - static double last = 0; - double elapsed = time_dt(); - string str, interactive; - - latency = (elapsed - last); - last = elapsed; - - double total_time, sample_time; - string status, substatus; - - progress.get_time(total_time, sample_time); - progress.get_status(status, substatus); - float progress_val = progress.get_progress(); - - if(substatus != "") - status += ": " + substatus; - - interactive = options.interactive? "On":"Off"; - - str = string_printf( - "%s" - " Time: %.2f" - " Latency: %.4f" - " Progress: %05.2f" - " Average: %.4f" - " Interactive: %s", - status.c_str(), total_time, latency, (double) progress_val*100, sample_time, interactive.c_str()); - - view_display_info(str.c_str()); - - if(options.show_help) - view_display_help(); + static double latency = 0.0; + static double last = 0; + double elapsed = time_dt(); + string str, interactive; + + latency = (elapsed - last); + last = elapsed; + + double total_time, sample_time; + string status, substatus; + + progress.get_time(total_time, sample_time); + progress.get_status(status, substatus); + float progress_val = progress.get_progress(); + + if (substatus != "") + status += ": " + substatus; + + interactive = options.interactive ? "On" : "Off"; + + str = string_printf( + "%s" + " Time: %.2f" + " Latency: %.4f" + " Progress: %05.2f" + " Average: %.4f" + " Interactive: %s", + status.c_str(), + total_time, + latency, + (double)progress_val * 100, + sample_time, + interactive.c_str()); + + view_display_info(str.c_str()); + + if (options.show_help) + view_display_help(); } static void display() { - static DeviceDrawParams draw_params = DeviceDrawParams(); + static DeviceDrawParams draw_params = DeviceDrawParams(); - options.session->draw(session_buffer_params(), draw_params); + options.session->draw(session_buffer_params(), draw_params); - display_info(options.session->progress); + display_info(options.session->progress); } static void motion(int x, int y, int button) { - if(options.interactive) { - Transform matrix = options.session->scene->camera->matrix; - - /* Translate */ - if(button == 0) { - float3 translate = make_float3(x * 0.01f, -(y * 0.01f), 0.0f); - matrix = matrix * transform_translate(translate); - } - - /* Rotate */ - else if(button == 2) { - float4 r1 = make_float4((float)x * 0.1f, 0.0f, 1.0f, 0.0f); - matrix = matrix * transform_rotate(DEG2RADF(r1.x), make_float3(r1.y, r1.z, r1.w)); - - float4 r2 = make_float4(y * 0.1f, 1.0f, 0.0f, 0.0f); - matrix = matrix * transform_rotate(DEG2RADF(r2.x), make_float3(r2.y, r2.z, r2.w)); - } - - /* Update and Reset */ - options.session->scene->camera->matrix = matrix; - options.session->scene->camera->need_update = true; - options.session->scene->camera->need_device_update = true; - - options.session->reset(session_buffer_params(), options.session_params.samples); - } + if (options.interactive) { + Transform matrix = options.session->scene->camera->matrix; + + /* Translate */ + if (button == 0) { + float3 translate = make_float3(x * 0.01f, -(y * 0.01f), 0.0f); + matrix = matrix * transform_translate(translate); + } + + /* Rotate */ + else if (button == 2) { + float4 r1 = make_float4((float)x * 0.1f, 0.0f, 1.0f, 0.0f); + matrix = matrix * transform_rotate(DEG2RADF(r1.x), make_float3(r1.y, r1.z, r1.w)); + + float4 r2 = make_float4(y * 0.1f, 1.0f, 0.0f, 0.0f); + matrix = matrix * transform_rotate(DEG2RADF(r2.x), make_float3(r2.y, r2.z, r2.w)); + } + + /* Update and Reset */ + options.session->scene->camera->matrix = matrix; + options.session->scene->camera->need_update = true; + options.session->scene->camera->need_device_update = true; + + options.session->reset(session_buffer_params(), options.session_params.samples); + } } static void resize(int width, int height) { - options.width = width; - options.height = height; - - if(options.session) { - /* Update camera */ - options.session->scene->camera->width = width; - options.session->scene->camera->height = height; - options.session->scene->camera->compute_auto_viewplane(); - options.session->scene->camera->need_update = true; - options.session->scene->camera->need_device_update = true; - - options.session->reset(session_buffer_params(), options.session_params.samples); - } + options.width = width; + options.height = height; + + if (options.session) { + /* Update camera */ + options.session->scene->camera->width = width; + options.session->scene->camera->height = height; + options.session->scene->camera->compute_auto_viewplane(); + options.session->scene->camera->need_update = true; + options.session->scene->camera->need_device_update = true; + + options.session->reset(session_buffer_params(), options.session_params.samples); + } } static void keyboard(unsigned char key) { - /* Toggle help */ - if(key == 'h') - options.show_help = !(options.show_help); - - /* Reset */ - else if(key == 'r') - options.session->reset(session_buffer_params(), options.session_params.samples); - - /* Cancel */ - else if(key == 27) // escape - options.session->progress.set_cancel("Canceled"); - - /* Pause */ - else if(key == 'p') { - options.pause = !options.pause; - options.session->set_pause(options.pause); - } - - /* Interactive Mode */ - else if(key == 'i') - options.interactive = !(options.interactive); - - /* Navigation */ - else if(options.interactive && (key == 'w' || key == 'a' || key == 's' || key == 'd')) { - Transform matrix = options.session->scene->camera->matrix; - float3 translate; - - if(key == 'w') - translate = make_float3(0.0f, 0.0f, 0.1f); - else if(key == 's') - translate = make_float3(0.0f, 0.0f, -0.1f); - else if(key == 'a') - translate = make_float3(-0.1f, 0.0f, 0.0f); - else if(key == 'd') - translate = make_float3(0.1f, 0.0f, 0.0f); - - matrix = matrix * transform_translate(translate); - - /* Update and Reset */ - options.session->scene->camera->matrix = matrix; - options.session->scene->camera->need_update = true; - options.session->scene->camera->need_device_update = true; - - options.session->reset(session_buffer_params(), options.session_params.samples); - } - - /* Set Max Bounces */ - else if(options.interactive && (key == '0' || key == '1' || key == '2' || key == '3')) { - int bounce; - switch(key) { - case '0': bounce = 0; break; - case '1': bounce = 1; break; - case '2': bounce = 2; break; - case '3': bounce = 3; break; - default: bounce = 0; break; - } - - options.session->scene->integrator->max_bounce = bounce; - - /* Update and Reset */ - options.session->scene->integrator->need_update = true; - - options.session->reset(session_buffer_params(), options.session_params.samples); - } + /* Toggle help */ + if (key == 'h') + options.show_help = !(options.show_help); + + /* Reset */ + else if (key == 'r') + options.session->reset(session_buffer_params(), options.session_params.samples); + + /* Cancel */ + else if (key == 27) // escape + options.session->progress.set_cancel("Canceled"); + + /* Pause */ + else if (key == 'p') { + options.pause = !options.pause; + options.session->set_pause(options.pause); + } + + /* Interactive Mode */ + else if (key == 'i') + options.interactive = !(options.interactive); + + /* Navigation */ + else if (options.interactive && (key == 'w' || key == 'a' || key == 's' || key == 'd')) { + Transform matrix = options.session->scene->camera->matrix; + float3 translate; + + if (key == 'w') + translate = make_float3(0.0f, 0.0f, 0.1f); + else if (key == 's') + translate = make_float3(0.0f, 0.0f, -0.1f); + else if (key == 'a') + translate = make_float3(-0.1f, 0.0f, 0.0f); + else if (key == 'd') + translate = make_float3(0.1f, 0.0f, 0.0f); + + matrix = matrix * transform_translate(translate); + + /* Update and Reset */ + options.session->scene->camera->matrix = matrix; + options.session->scene->camera->need_update = true; + options.session->scene->camera->need_device_update = true; + + options.session->reset(session_buffer_params(), options.session_params.samples); + } + + /* Set Max Bounces */ + else if (options.interactive && (key == '0' || key == '1' || key == '2' || key == '3')) { + int bounce; + switch (key) { + case '0': + bounce = 0; + break; + case '1': + bounce = 1; + break; + case '2': + bounce = 2; + break; + case '3': + bounce = 3; + break; + default: + bounce = 0; + break; + } + + options.session->scene->integrator->max_bounce = bounce; + + /* Update and Reset */ + options.session->scene->integrator->need_update = true; + + options.session->reset(session_buffer_params(), options.session_params.samples); + } } #endif static int files_parse(int argc, const char *argv[]) { - if(argc > 0) - options.filepath = argv[0]; + if (argc > 0) + options.filepath = argv[0]; - return 0; + return 0; } static void options_parse(int argc, const char **argv) { - options.width = 0; - options.height = 0; - options.filepath = ""; - options.session = NULL; - options.quiet = false; - - /* device names */ - string device_names = ""; - string devicename = "CPU"; - bool list = false; - - /* List devices for which support is compiled in. */ - vector types = Device::available_types(); - foreach(DeviceType type, types) { - if(device_names != "") - device_names += ", "; - - device_names += Device::string_from_type(type); - } - - /* shading system */ - string ssname = "svm"; - - /* parse options */ - ArgParse ap; - bool help = false, debug = false, version = false; - int verbosity = 1; - - ap.options ("Usage: cycles [options] file.xml", - "%*", files_parse, "", - "--device %s", &devicename, ("Devices to use: " + device_names).c_str(), + options.width = 0; + options.height = 0; + options.filepath = ""; + options.session = NULL; + options.quiet = false; + + /* device names */ + string device_names = ""; + string devicename = "CPU"; + bool list = false; + + /* List devices for which support is compiled in. */ + vector types = Device::available_types(); + foreach (DeviceType type, types) { + if (device_names != "") + device_names += ", "; + + device_names += Device::string_from_type(type); + } + + /* shading system */ + string ssname = "svm"; + + /* parse options */ + ArgParse ap; + bool help = false, debug = false, version = false; + int verbosity = 1; + + ap.options("Usage: cycles [options] file.xml", + "%*", + files_parse, + "", + "--device %s", + &devicename, + ("Devices to use: " + device_names).c_str(), #ifdef WITH_OSL - "--shadingsys %s", &ssname, "Shading system to use: svm, osl", + "--shadingsys %s", + &ssname, + "Shading system to use: svm, osl", #endif - "--background", &options.session_params.background, "Render in background, without user interface", - "--quiet", &options.quiet, "In background mode, don't print progress messages", - "--samples %d", &options.session_params.samples, "Number of samples to render", - "--output %s", &options.output_path, "File path to write output image", - "--threads %d", &options.session_params.threads, "CPU Rendering Threads", - "--width %d", &options.width, "Window width in pixel", - "--height %d", &options.height, "Window height in pixel", - "--tile-width %d", &options.session_params.tile_size.x, "Tile width in pixels", - "--tile-height %d", &options.session_params.tile_size.y, "Tile height in pixels", - "--list-devices", &list, "List information about all available devices", + "--background", + &options.session_params.background, + "Render in background, without user interface", + "--quiet", + &options.quiet, + "In background mode, don't print progress messages", + "--samples %d", + &options.session_params.samples, + "Number of samples to render", + "--output %s", + &options.output_path, + "File path to write output image", + "--threads %d", + &options.session_params.threads, + "CPU Rendering Threads", + "--width %d", + &options.width, + "Window width in pixel", + "--height %d", + &options.height, + "Window height in pixel", + "--tile-width %d", + &options.session_params.tile_size.x, + "Tile width in pixels", + "--tile-height %d", + &options.session_params.tile_size.y, + "Tile height in pixels", + "--list-devices", + &list, + "List information about all available devices", #ifdef WITH_CYCLES_LOGGING - "--debug", &debug, "Enable debug logging", - "--verbose %d", &verbosity, "Set verbosity of the logger", + "--debug", + &debug, + "Enable debug logging", + "--verbose %d", + &verbosity, + "Set verbosity of the logger", #endif - "--help", &help, "Print help message", - "--version", &version, "Print version number", - NULL); - - if(ap.parse(argc, argv) < 0) { - fprintf(stderr, "%s\n", ap.geterror().c_str()); - ap.usage(); - exit(EXIT_FAILURE); - } - - if(debug) { - util_logging_start(); - util_logging_verbosity_set(verbosity); - } - - if(list) { - vector devices = Device::available_devices(); - printf("Devices:\n"); - - foreach(DeviceInfo& info, devices) { - printf(" %-10s%s%s\n", - Device::string_from_type(info.type).c_str(), - info.description.c_str(), - (info.display_device)? " (display)": ""); - } - - exit(EXIT_SUCCESS); - } - else if(version) { - printf("%s\n", CYCLES_VERSION_STRING); - exit(EXIT_SUCCESS); - } - else if(help || options.filepath == "") { - ap.usage(); - exit(EXIT_SUCCESS); - } - - if(ssname == "osl") - options.scene_params.shadingsystem = SHADINGSYSTEM_OSL; - else if(ssname == "svm") - options.scene_params.shadingsystem = SHADINGSYSTEM_SVM; + "--help", + &help, + "Print help message", + "--version", + &version, + "Print version number", + NULL); + + if (ap.parse(argc, argv) < 0) { + fprintf(stderr, "%s\n", ap.geterror().c_str()); + ap.usage(); + exit(EXIT_FAILURE); + } + + if (debug) { + util_logging_start(); + util_logging_verbosity_set(verbosity); + } + + if (list) { + vector devices = Device::available_devices(); + printf("Devices:\n"); + + foreach (DeviceInfo &info, devices) { + printf(" %-10s%s%s\n", + Device::string_from_type(info.type).c_str(), + info.description.c_str(), + (info.display_device) ? " (display)" : ""); + } + + exit(EXIT_SUCCESS); + } + else if (version) { + printf("%s\n", CYCLES_VERSION_STRING); + exit(EXIT_SUCCESS); + } + else if (help || options.filepath == "") { + ap.usage(); + exit(EXIT_SUCCESS); + } + + if (ssname == "osl") + options.scene_params.shadingsystem = SHADINGSYSTEM_OSL; + else if (ssname == "svm") + options.scene_params.shadingsystem = SHADINGSYSTEM_SVM; #ifndef WITH_CYCLES_STANDALONE_GUI - options.session_params.background = true; + options.session_params.background = true; #endif - /* Use progressive rendering */ - options.session_params.progressive = true; + /* Use progressive rendering */ + options.session_params.progressive = true; - /* find matching device */ - DeviceType device_type = Device::type_from_string(devicename.c_str()); - vector devices = Device::available_devices(DEVICE_MASK(device_type)); + /* find matching device */ + DeviceType device_type = Device::type_from_string(devicename.c_str()); + vector devices = Device::available_devices(DEVICE_MASK(device_type)); - bool device_available = false; - if (!devices.empty()) { - options.session_params.device = devices.front(); - device_available = true; - } + bool device_available = false; + if (!devices.empty()) { + options.session_params.device = devices.front(); + device_available = true; + } - /* handle invalid configurations */ - if(options.session_params.device.type == DEVICE_NONE || !device_available) { - fprintf(stderr, "Unknown device: %s\n", devicename.c_str()); - exit(EXIT_FAILURE); - } + /* handle invalid configurations */ + if (options.session_params.device.type == DEVICE_NONE || !device_available) { + fprintf(stderr, "Unknown device: %s\n", devicename.c_str()); + exit(EXIT_FAILURE); + } #ifdef WITH_OSL - else if(!(ssname == "osl" || ssname == "svm")) { - fprintf(stderr, "Unknown shading system: %s\n", ssname.c_str()); - exit(EXIT_FAILURE); - } - else if(options.scene_params.shadingsystem == SHADINGSYSTEM_OSL && options.session_params.device.type != DEVICE_CPU) { - fprintf(stderr, "OSL shading system only works with CPU device\n"); - exit(EXIT_FAILURE); - } + else if (!(ssname == "osl" || ssname == "svm")) { + fprintf(stderr, "Unknown shading system: %s\n", ssname.c_str()); + exit(EXIT_FAILURE); + } + else if (options.scene_params.shadingsystem == SHADINGSYSTEM_OSL && + options.session_params.device.type != DEVICE_CPU) { + fprintf(stderr, "OSL shading system only works with CPU device\n"); + exit(EXIT_FAILURE); + } #endif - else if(options.session_params.samples < 0) { - fprintf(stderr, "Invalid number of samples: %d\n", options.session_params.samples); - exit(EXIT_FAILURE); - } - else if(options.filepath == "") { - fprintf(stderr, "No file path specified\n"); - exit(EXIT_FAILURE); - } - - /* For smoother Viewport */ - options.session_params.start_resolution = 64; + else if (options.session_params.samples < 0) { + fprintf(stderr, "Invalid number of samples: %d\n", options.session_params.samples); + exit(EXIT_FAILURE); + } + else if (options.filepath == "") { + fprintf(stderr, "No file path specified\n"); + exit(EXIT_FAILURE); + } + + /* For smoother Viewport */ + options.session_params.start_resolution = 64; } CCL_NAMESPACE_END @@ -493,26 +540,33 @@ using namespace ccl; int main(int argc, const char **argv) { - util_logging_init(argv[0]); - path_init(); - options_parse(argc, argv); + util_logging_init(argv[0]); + path_init(); + options_parse(argc, argv); #ifdef WITH_CYCLES_STANDALONE_GUI - if(options.session_params.background) { + if (options.session_params.background) { #endif - session_init(); - options.session->wait(); - session_exit(); + session_init(); + options.session->wait(); + session_exit(); #ifdef WITH_CYCLES_STANDALONE_GUI - } - else { - string title = "Cycles: " + path_filename(options.filepath); - - /* init/exit are callback so they run while GL is initialized */ - view_main_loop(title.c_str(), options.width, options.height, - session_init, session_exit, resize, display, keyboard, motion); - } + } + else { + string title = "Cycles: " + path_filename(options.filepath); + + /* init/exit are callback so they run while GL is initialized */ + view_main_loop(title.c_str(), + options.width, + options.height, + session_init, + session_exit, + resize, + display, + keyboard, + motion); + } #endif - return 0; + return 0; } diff --git a/intern/cycles/app/cycles_xml.cpp b/intern/cycles/app/cycles_xml.cpp index 86491adb3a4..9caf7068d3d 100644 --- a/intern/cycles/app/cycles_xml.cpp +++ b/intern/cycles/app/cycles_xml.cpp @@ -51,647 +51,649 @@ CCL_NAMESPACE_BEGIN /* XML reading state */ struct XMLReadState : public XMLReader { - Scene *scene; /* scene pointer */ - Transform tfm; /* current transform state */ - bool smooth; /* smooth normal state */ - Shader *shader; /* current shader */ - string base; /* base path to current file*/ - float dicing_rate; /* current dicing rate */ - - XMLReadState() - : scene(NULL), - smooth(false), - shader(NULL), - dicing_rate(1.0f) - { - tfm = transform_identity(); - } + Scene *scene; /* scene pointer */ + Transform tfm; /* current transform state */ + bool smooth; /* smooth normal state */ + Shader *shader; /* current shader */ + string base; /* base path to current file*/ + float dicing_rate; /* current dicing rate */ + + XMLReadState() : scene(NULL), smooth(false), shader(NULL), dicing_rate(1.0f) + { + tfm = transform_identity(); + } }; /* Attribute Reading */ static bool xml_read_int(int *value, xml_node node, const char *name) { - xml_attribute attr = node.attribute(name); + xml_attribute attr = node.attribute(name); - if(attr) { - *value = atoi(attr.value()); - return true; - } + if (attr) { + *value = atoi(attr.value()); + return true; + } - return false; + return false; } -static bool xml_read_int_array(vector& value, xml_node node, const char *name) +static bool xml_read_int_array(vector &value, xml_node node, const char *name) { - xml_attribute attr = node.attribute(name); + xml_attribute attr = node.attribute(name); - if(attr) { - vector tokens; - string_split(tokens, attr.value()); + if (attr) { + vector tokens; + string_split(tokens, attr.value()); - foreach(const string& token, tokens) - value.push_back(atoi(token.c_str())); + foreach (const string &token, tokens) + value.push_back(atoi(token.c_str())); - return true; - } + return true; + } - return false; + return false; } static bool xml_read_float(float *value, xml_node node, const char *name) { - xml_attribute attr = node.attribute(name); + xml_attribute attr = node.attribute(name); - if(attr) { - *value = (float)atof(attr.value()); - return true; - } + if (attr) { + *value = (float)atof(attr.value()); + return true; + } - return false; + return false; } -static bool xml_read_float_array(vector& value, xml_node node, const char *name) +static bool xml_read_float_array(vector &value, xml_node node, const char *name) { - xml_attribute attr = node.attribute(name); + xml_attribute attr = node.attribute(name); - if(attr) { - vector tokens; - string_split(tokens, attr.value()); + if (attr) { + vector tokens; + string_split(tokens, attr.value()); - foreach(const string& token, tokens) - value.push_back((float)atof(token.c_str())); + foreach (const string &token, tokens) + value.push_back((float)atof(token.c_str())); - return true; - } + return true; + } - return false; + return false; } static bool xml_read_float3(float3 *value, xml_node node, const char *name) { - vector array; + vector array; - if(xml_read_float_array(array, node, name) && array.size() == 3) { - *value = make_float3(array[0], array[1], array[2]); - return true; - } + if (xml_read_float_array(array, node, name) && array.size() == 3) { + *value = make_float3(array[0], array[1], array[2]); + return true; + } - return false; + return false; } -static bool xml_read_float3_array(vector& value, xml_node node, const char *name) +static bool xml_read_float3_array(vector &value, xml_node node, const char *name) { - vector array; + vector array; - if(xml_read_float_array(array, node, name)) { - for(size_t i = 0; i < array.size(); i += 3) - value.push_back(make_float3(array[i+0], array[i+1], array[i+2])); + if (xml_read_float_array(array, node, name)) { + for (size_t i = 0; i < array.size(); i += 3) + value.push_back(make_float3(array[i + 0], array[i + 1], array[i + 2])); - return true; - } + return true; + } - return false; + return false; } static bool xml_read_float4(float4 *value, xml_node node, const char *name) { - vector array; + vector array; - if(xml_read_float_array(array, node, name) && array.size() == 4) { - *value = make_float4(array[0], array[1], array[2], array[3]); - return true; - } + if (xml_read_float_array(array, node, name) && array.size() == 4) { + *value = make_float4(array[0], array[1], array[2], array[3]); + return true; + } - return false; + return false; } static bool xml_read_string(string *str, xml_node node, const char *name) { - xml_attribute attr = node.attribute(name); + xml_attribute attr = node.attribute(name); - if(attr) { - *str = attr.value(); - return true; - } + if (attr) { + *str = attr.value(); + return true; + } - return false; + return false; } static bool xml_equal_string(xml_node node, const char *name, const char *value) { - xml_attribute attr = node.attribute(name); + xml_attribute attr = node.attribute(name); - if(attr) - return string_iequals(attr.value(), value); + if (attr) + return string_iequals(attr.value(), value); - return false; + return false; } /* Camera */ -static void xml_read_camera(XMLReadState& state, xml_node node) +static void xml_read_camera(XMLReadState &state, xml_node node) { - Camera *cam = state.scene->camera; + Camera *cam = state.scene->camera; - xml_read_int(&cam->width, node, "width"); - xml_read_int(&cam->height, node, "height"); + xml_read_int(&cam->width, node, "width"); + xml_read_int(&cam->height, node, "height"); - cam->full_width = cam->width; - cam->full_height = cam->height; + cam->full_width = cam->width; + cam->full_height = cam->height; - xml_read_node(state, cam, node); + xml_read_node(state, cam, node); - cam->matrix = state.tfm; + cam->matrix = state.tfm; - cam->need_update = true; - cam->update(state.scene); + cam->need_update = true; + cam->update(state.scene); } /* Shader */ -static void xml_read_shader_graph(XMLReadState& state, Shader *shader, xml_node graph_node) +static void xml_read_shader_graph(XMLReadState &state, Shader *shader, xml_node graph_node) { - xml_read_node(state, shader, graph_node); - - ShaderGraph *graph = new ShaderGraph(); - - /* local state, shader nodes can't link to nodes outside the shader graph */ - XMLReader graph_reader; - graph_reader.node_map[ustring("output")] = graph->output(); - - for(xml_node node = graph_node.first_child(); node; node = node.next_sibling()) { - ustring node_name(node.name()); - - if(node_name == "connect") { - /* connect nodes */ - vector from_tokens, to_tokens; - - string_split(from_tokens, node.attribute("from").value()); - string_split(to_tokens, node.attribute("to").value()); - - if(from_tokens.size() == 2 && to_tokens.size() == 2) { - ustring from_node_name(from_tokens[0]); - ustring from_socket_name(from_tokens[1]); - ustring to_node_name(to_tokens[0]); - ustring to_socket_name(to_tokens[1]); - - /* find nodes and sockets */ - ShaderOutput *output = NULL; - ShaderInput *input = NULL; - - if(graph_reader.node_map.find(from_node_name) != graph_reader.node_map.end()) { - ShaderNode *fromnode = (ShaderNode*)graph_reader.node_map[from_node_name]; - - foreach(ShaderOutput *out, fromnode->outputs) - if(string_iequals(out->socket_type.name.string(), from_socket_name.string())) - output = out; - - if(!output) - fprintf(stderr, "Unknown output socket name \"%s\" on \"%s\".\n", from_node_name.c_str(), from_socket_name.c_str()); - } - else - fprintf(stderr, "Unknown shader node name \"%s\".\n", from_node_name.c_str()); - - if(graph_reader.node_map.find(to_node_name) != graph_reader.node_map.end()) { - ShaderNode *tonode = (ShaderNode*)graph_reader.node_map[to_node_name]; - - foreach(ShaderInput *in, tonode->inputs) - if(string_iequals(in->socket_type.name.string(), to_socket_name.string())) - input = in; - - if(!input) - fprintf(stderr, "Unknown input socket name \"%s\" on \"%s\".\n", to_socket_name.c_str(), to_node_name.c_str()); - } - else - fprintf(stderr, "Unknown shader node name \"%s\".\n", to_node_name.c_str()); - - /* connect */ - if(output && input) - graph->connect(output, input); - } - else - fprintf(stderr, "Invalid from or to value for connect node.\n"); - - continue; - } - - ShaderNode *snode = NULL; + xml_read_node(state, shader, graph_node); + + ShaderGraph *graph = new ShaderGraph(); + + /* local state, shader nodes can't link to nodes outside the shader graph */ + XMLReader graph_reader; + graph_reader.node_map[ustring("output")] = graph->output(); + + for (xml_node node = graph_node.first_child(); node; node = node.next_sibling()) { + ustring node_name(node.name()); + + if (node_name == "connect") { + /* connect nodes */ + vector from_tokens, to_tokens; + + string_split(from_tokens, node.attribute("from").value()); + string_split(to_tokens, node.attribute("to").value()); + + if (from_tokens.size() == 2 && to_tokens.size() == 2) { + ustring from_node_name(from_tokens[0]); + ustring from_socket_name(from_tokens[1]); + ustring to_node_name(to_tokens[0]); + ustring to_socket_name(to_tokens[1]); + + /* find nodes and sockets */ + ShaderOutput *output = NULL; + ShaderInput *input = NULL; + + if (graph_reader.node_map.find(from_node_name) != graph_reader.node_map.end()) { + ShaderNode *fromnode = (ShaderNode *)graph_reader.node_map[from_node_name]; + + foreach (ShaderOutput *out, fromnode->outputs) + if (string_iequals(out->socket_type.name.string(), from_socket_name.string())) + output = out; + + if (!output) + fprintf(stderr, + "Unknown output socket name \"%s\" on \"%s\".\n", + from_node_name.c_str(), + from_socket_name.c_str()); + } + else + fprintf(stderr, "Unknown shader node name \"%s\".\n", from_node_name.c_str()); + + if (graph_reader.node_map.find(to_node_name) != graph_reader.node_map.end()) { + ShaderNode *tonode = (ShaderNode *)graph_reader.node_map[to_node_name]; + + foreach (ShaderInput *in, tonode->inputs) + if (string_iequals(in->socket_type.name.string(), to_socket_name.string())) + input = in; + + if (!input) + fprintf(stderr, + "Unknown input socket name \"%s\" on \"%s\".\n", + to_socket_name.c_str(), + to_node_name.c_str()); + } + else + fprintf(stderr, "Unknown shader node name \"%s\".\n", to_node_name.c_str()); + + /* connect */ + if (output && input) + graph->connect(output, input); + } + else + fprintf(stderr, "Invalid from or to value for connect node.\n"); + + continue; + } + + ShaderNode *snode = NULL; #ifdef WITH_OSL - if(node_name == "osl_shader") { - ShaderManager *manager = state.scene->shader_manager; - - if(manager->use_osl()) { - std::string filepath; - - if(xml_read_string(&filepath, node, "src")) { - if(path_is_relative(filepath)) { - filepath = path_join(state.base, filepath); - } - - snode = ((OSLShaderManager*)manager)->osl_node(filepath); - - if(!snode) { - fprintf(stderr, "Failed to create OSL node from \"%s\".\n", filepath.c_str()); - continue; - } - } - else { - fprintf(stderr, "OSL node missing \"src\" attribute.\n"); - continue; - } - } - else { - fprintf(stderr, "OSL node without using --shadingsys osl.\n"); - continue; - } - } - else + if (node_name == "osl_shader") { + ShaderManager *manager = state.scene->shader_manager; + + if (manager->use_osl()) { + std::string filepath; + + if (xml_read_string(&filepath, node, "src")) { + if (path_is_relative(filepath)) { + filepath = path_join(state.base, filepath); + } + + snode = ((OSLShaderManager *)manager)->osl_node(filepath); + + if (!snode) { + fprintf(stderr, "Failed to create OSL node from \"%s\".\n", filepath.c_str()); + continue; + } + } + else { + fprintf(stderr, "OSL node missing \"src\" attribute.\n"); + continue; + } + } + else { + fprintf(stderr, "OSL node without using --shadingsys osl.\n"); + continue; + } + } + else #endif - { - /* exception for name collision */ - if(node_name == "background") - node_name = "background_shader"; - - const NodeType *node_type = NodeType::find(node_name); - - if(!node_type) { - fprintf(stderr, "Unknown shader node \"%s\".\n", node.name()); - continue; - } - else if(node_type->type != NodeType::SHADER) { - fprintf(stderr, "Node type \"%s\" is not a shader node.\n", node_type->name.c_str()); - continue; - } - - snode = (ShaderNode*) node_type->create(node_type); - } - - xml_read_node(graph_reader, snode, node); - - if(node_name == "image_texture") { - ImageTextureNode *img = (ImageTextureNode*) snode; - img->filename = path_join(state.base, img->filename.string()); - } - else if(node_name == "environment_texture") { - EnvironmentTextureNode *env = (EnvironmentTextureNode*) snode; - env->filename = path_join(state.base, env->filename.string()); - } - - if(snode) { - /* add to graph */ - graph->add(snode); - } - } - - shader->set_graph(graph); - shader->tag_update(state.scene); + { + /* exception for name collision */ + if (node_name == "background") + node_name = "background_shader"; + + const NodeType *node_type = NodeType::find(node_name); + + if (!node_type) { + fprintf(stderr, "Unknown shader node \"%s\".\n", node.name()); + continue; + } + else if (node_type->type != NodeType::SHADER) { + fprintf(stderr, "Node type \"%s\" is not a shader node.\n", node_type->name.c_str()); + continue; + } + + snode = (ShaderNode *)node_type->create(node_type); + } + + xml_read_node(graph_reader, snode, node); + + if (node_name == "image_texture") { + ImageTextureNode *img = (ImageTextureNode *)snode; + img->filename = path_join(state.base, img->filename.string()); + } + else if (node_name == "environment_texture") { + EnvironmentTextureNode *env = (EnvironmentTextureNode *)snode; + env->filename = path_join(state.base, env->filename.string()); + } + + if (snode) { + /* add to graph */ + graph->add(snode); + } + } + + shader->set_graph(graph); + shader->tag_update(state.scene); } -static void xml_read_shader(XMLReadState& state, xml_node node) +static void xml_read_shader(XMLReadState &state, xml_node node) { - Shader *shader = new Shader(); - xml_read_shader_graph(state, shader, node); - state.scene->shaders.push_back(shader); + Shader *shader = new Shader(); + xml_read_shader_graph(state, shader, node); + state.scene->shaders.push_back(shader); } /* Background */ -static void xml_read_background(XMLReadState& state, xml_node node) +static void xml_read_background(XMLReadState &state, xml_node node) { - /* Background Settings */ - xml_read_node(state, state.scene->background, node); + /* Background Settings */ + xml_read_node(state, state.scene->background, node); - /* Background Shader */ - Shader *shader = state.scene->default_background; - xml_read_shader_graph(state, shader, node); + /* Background Shader */ + Shader *shader = state.scene->default_background; + xml_read_shader_graph(state, shader, node); } /* Mesh */ -static Mesh *xml_add_mesh(Scene *scene, const Transform& tfm) +static Mesh *xml_add_mesh(Scene *scene, const Transform &tfm) { - /* create mesh */ - Mesh *mesh = new Mesh(); - scene->meshes.push_back(mesh); + /* create mesh */ + Mesh *mesh = new Mesh(); + scene->meshes.push_back(mesh); - /* create object*/ - Object *object = new Object(); - object->mesh = mesh; - object->tfm = tfm; - scene->objects.push_back(object); + /* create object*/ + Object *object = new Object(); + object->mesh = mesh; + object->tfm = tfm; + scene->objects.push_back(object); - return mesh; + return mesh; } -static void xml_read_mesh(const XMLReadState& state, xml_node node) +static void xml_read_mesh(const XMLReadState &state, xml_node node) { - /* add mesh */ - Mesh *mesh = xml_add_mesh(state.scene, state.tfm); - mesh->used_shaders.push_back(state.shader); - - /* read state */ - int shader = 0; - bool smooth = state.smooth; - - /* read vertices and polygons */ - vector P; - vector UV; - vector verts, nverts; - - xml_read_float3_array(P, node, "P"); - xml_read_int_array(verts, node, "verts"); - xml_read_int_array(nverts, node, "nverts"); - - if(xml_equal_string(node, "subdivision", "catmull-clark")) { - mesh->subdivision_type = Mesh::SUBDIVISION_CATMULL_CLARK; - } - else if(xml_equal_string(node, "subdivision", "linear")) { - mesh->subdivision_type = Mesh::SUBDIVISION_LINEAR; - } - - if(mesh->subdivision_type == Mesh::SUBDIVISION_NONE) { - /* create vertices */ - mesh->verts = P; - - size_t num_triangles = 0; - for(size_t i = 0; i < nverts.size(); i++) - num_triangles += nverts[i]-2; - mesh->reserve_mesh(mesh->verts.size(), num_triangles); - - /* create triangles */ - int index_offset = 0; - - for(size_t i = 0; i < nverts.size(); i++) { - for(int j = 0; j < nverts[i]-2; j++) { - int v0 = verts[index_offset]; - int v1 = verts[index_offset + j + 1]; - int v2 = verts[index_offset + j + 2]; - - assert(v0 < (int)P.size()); - assert(v1 < (int)P.size()); - assert(v2 < (int)P.size()); - - mesh->add_triangle(v0, v1, v2, shader, smooth); - } - - index_offset += nverts[i]; - } - - if(xml_read_float_array(UV, node, "UV")) { - ustring name = ustring("UVMap"); - Attribute *attr = mesh->attributes.add(ATTR_STD_UV, name); - float2 *fdata = attr->data_float2(); - - /* loop over the triangles */ - index_offset = 0; - for(size_t i = 0; i < nverts.size(); i++) { - for(int j = 0; j < nverts[i]-2; j++) { - int v0 = index_offset; - int v1 = index_offset + j + 1; - int v2 = index_offset + j + 2; - - assert(v0*2+1 < (int)UV.size()); - assert(v1*2+1 < (int)UV.size()); - assert(v2*2+1 < (int)UV.size()); - - fdata[0] = make_float2(UV[v0*2], UV[v0*2+1]); - fdata[1] = make_float2(UV[v1*2], UV[v1*2+1]); - fdata[2] = make_float2(UV[v2*2], UV[v2*2+1]); - fdata += 3; - } - - index_offset += nverts[i]; - } - } - } - else { - /* create vertices */ - mesh->verts = P; - - size_t num_ngons = 0; - size_t num_corners = 0; - for(size_t i = 0; i < nverts.size(); i++) { - num_ngons += (nverts[i] == 4) ? 0 : 1; - num_corners += nverts[i]; - } - mesh->reserve_subd_faces(nverts.size(), num_ngons, num_corners); - - /* create subd_faces */ - int index_offset = 0; - - for(size_t i = 0; i < nverts.size(); i++) { - mesh->add_subd_face(&verts[index_offset], nverts[i], shader, smooth); - index_offset += nverts[i]; - } - - /* uv map */ - if(xml_read_float_array(UV, node, "UV")) { - ustring name = ustring("UVMap"); - Attribute *attr = mesh->subd_attributes.add(ATTR_STD_UV, name); - float3 *fdata = attr->data_float3(); + /* add mesh */ + Mesh *mesh = xml_add_mesh(state.scene, state.tfm); + mesh->used_shaders.push_back(state.shader); + + /* read state */ + int shader = 0; + bool smooth = state.smooth; + + /* read vertices and polygons */ + vector P; + vector UV; + vector verts, nverts; + + xml_read_float3_array(P, node, "P"); + xml_read_int_array(verts, node, "verts"); + xml_read_int_array(nverts, node, "nverts"); + + if (xml_equal_string(node, "subdivision", "catmull-clark")) { + mesh->subdivision_type = Mesh::SUBDIVISION_CATMULL_CLARK; + } + else if (xml_equal_string(node, "subdivision", "linear")) { + mesh->subdivision_type = Mesh::SUBDIVISION_LINEAR; + } + + if (mesh->subdivision_type == Mesh::SUBDIVISION_NONE) { + /* create vertices */ + mesh->verts = P; + + size_t num_triangles = 0; + for (size_t i = 0; i < nverts.size(); i++) + num_triangles += nverts[i] - 2; + mesh->reserve_mesh(mesh->verts.size(), num_triangles); + + /* create triangles */ + int index_offset = 0; + + for (size_t i = 0; i < nverts.size(); i++) { + for (int j = 0; j < nverts[i] - 2; j++) { + int v0 = verts[index_offset]; + int v1 = verts[index_offset + j + 1]; + int v2 = verts[index_offset + j + 2]; + + assert(v0 < (int)P.size()); + assert(v1 < (int)P.size()); + assert(v2 < (int)P.size()); + + mesh->add_triangle(v0, v1, v2, shader, smooth); + } + + index_offset += nverts[i]; + } + + if (xml_read_float_array(UV, node, "UV")) { + ustring name = ustring("UVMap"); + Attribute *attr = mesh->attributes.add(ATTR_STD_UV, name); + float2 *fdata = attr->data_float2(); + + /* loop over the triangles */ + index_offset = 0; + for (size_t i = 0; i < nverts.size(); i++) { + for (int j = 0; j < nverts[i] - 2; j++) { + int v0 = index_offset; + int v1 = index_offset + j + 1; + int v2 = index_offset + j + 2; + + assert(v0 * 2 + 1 < (int)UV.size()); + assert(v1 * 2 + 1 < (int)UV.size()); + assert(v2 * 2 + 1 < (int)UV.size()); + + fdata[0] = make_float2(UV[v0 * 2], UV[v0 * 2 + 1]); + fdata[1] = make_float2(UV[v1 * 2], UV[v1 * 2 + 1]); + fdata[2] = make_float2(UV[v2 * 2], UV[v2 * 2 + 1]); + fdata += 3; + } + + index_offset += nverts[i]; + } + } + } + else { + /* create vertices */ + mesh->verts = P; + + size_t num_ngons = 0; + size_t num_corners = 0; + for (size_t i = 0; i < nverts.size(); i++) { + num_ngons += (nverts[i] == 4) ? 0 : 1; + num_corners += nverts[i]; + } + mesh->reserve_subd_faces(nverts.size(), num_ngons, num_corners); + + /* create subd_faces */ + int index_offset = 0; + + for (size_t i = 0; i < nverts.size(); i++) { + mesh->add_subd_face(&verts[index_offset], nverts[i], shader, smooth); + index_offset += nverts[i]; + } + + /* uv map */ + if (xml_read_float_array(UV, node, "UV")) { + ustring name = ustring("UVMap"); + Attribute *attr = mesh->subd_attributes.add(ATTR_STD_UV, name); + float3 *fdata = attr->data_float3(); #if 0 - if(subdivide_uvs) { - attr->flags |= ATTR_SUBDIVIDED; - } + if(subdivide_uvs) { + attr->flags |= ATTR_SUBDIVIDED; + } #endif - index_offset = 0; - for(size_t i = 0; i < nverts.size(); i++) { - for(int j = 0; j < nverts[i]; j++) { - *(fdata++) = make_float3(UV[index_offset++]); - } - } - } - - /* setup subd params */ - if(!mesh->subd_params) { - mesh->subd_params = new SubdParams(mesh); - } - SubdParams& sdparams = *mesh->subd_params; - - sdparams.dicing_rate = state.dicing_rate; - xml_read_float(&sdparams.dicing_rate, node, "dicing_rate"); - sdparams.dicing_rate = std::max(0.1f, sdparams.dicing_rate); - - sdparams.objecttoworld = state.tfm; - } - - /* we don't yet support arbitrary attributes, for now add vertex - * coordinates as generated coordinates if requested */ - if(mesh->need_attribute(state.scene, ATTR_STD_GENERATED)) { - Attribute *attr = mesh->attributes.add(ATTR_STD_GENERATED); - memcpy(attr->data_float3(), mesh->verts.data(), sizeof(float3)*mesh->verts.size()); - } + index_offset = 0; + for (size_t i = 0; i < nverts.size(); i++) { + for (int j = 0; j < nverts[i]; j++) { + *(fdata++) = make_float3(UV[index_offset++]); + } + } + } + + /* setup subd params */ + if (!mesh->subd_params) { + mesh->subd_params = new SubdParams(mesh); + } + SubdParams &sdparams = *mesh->subd_params; + + sdparams.dicing_rate = state.dicing_rate; + xml_read_float(&sdparams.dicing_rate, node, "dicing_rate"); + sdparams.dicing_rate = std::max(0.1f, sdparams.dicing_rate); + + sdparams.objecttoworld = state.tfm; + } + + /* we don't yet support arbitrary attributes, for now add vertex + * coordinates as generated coordinates if requested */ + if (mesh->need_attribute(state.scene, ATTR_STD_GENERATED)) { + Attribute *attr = mesh->attributes.add(ATTR_STD_GENERATED); + memcpy(attr->data_float3(), mesh->verts.data(), sizeof(float3) * mesh->verts.size()); + } } /* Light */ -static void xml_read_light(XMLReadState& state, xml_node node) +static void xml_read_light(XMLReadState &state, xml_node node) { - Light *light = new Light(); + Light *light = new Light(); - light->shader = state.shader; - xml_read_node(state, light, node); + light->shader = state.shader; + xml_read_node(state, light, node); - state.scene->lights.push_back(light); + state.scene->lights.push_back(light); } /* Transform */ -static void xml_read_transform(xml_node node, Transform& tfm) +static void xml_read_transform(xml_node node, Transform &tfm) { - if(node.attribute("matrix")) { - vector matrix; - if(xml_read_float_array(matrix, node, "matrix") && matrix.size() == 16) { - ProjectionTransform projection = *(ProjectionTransform*)&matrix[0]; - tfm = tfm * projection_to_transform(projection_transpose(projection)); - } - } - - if(node.attribute("translate")) { - float3 translate = make_float3(0.0f, 0.0f, 0.0f); - xml_read_float3(&translate, node, "translate"); - tfm = tfm * transform_translate(translate); - } - - if(node.attribute("rotate")) { - float4 rotate = make_float4(0.0f, 0.0f, 0.0f, 0.0f); - xml_read_float4(&rotate, node, "rotate"); - tfm = tfm * transform_rotate(DEG2RADF(rotate.x), make_float3(rotate.y, rotate.z, rotate.w)); - } - - if(node.attribute("scale")) { - float3 scale = make_float3(0.0f, 0.0f, 0.0f); - xml_read_float3(&scale, node, "scale"); - tfm = tfm * transform_scale(scale); - } + if (node.attribute("matrix")) { + vector matrix; + if (xml_read_float_array(matrix, node, "matrix") && matrix.size() == 16) { + ProjectionTransform projection = *(ProjectionTransform *)&matrix[0]; + tfm = tfm * projection_to_transform(projection_transpose(projection)); + } + } + + if (node.attribute("translate")) { + float3 translate = make_float3(0.0f, 0.0f, 0.0f); + xml_read_float3(&translate, node, "translate"); + tfm = tfm * transform_translate(translate); + } + + if (node.attribute("rotate")) { + float4 rotate = make_float4(0.0f, 0.0f, 0.0f, 0.0f); + xml_read_float4(&rotate, node, "rotate"); + tfm = tfm * transform_rotate(DEG2RADF(rotate.x), make_float3(rotate.y, rotate.z, rotate.w)); + } + + if (node.attribute("scale")) { + float3 scale = make_float3(0.0f, 0.0f, 0.0f); + xml_read_float3(&scale, node, "scale"); + tfm = tfm * transform_scale(scale); + } } /* State */ -static void xml_read_state(XMLReadState& state, xml_node node) +static void xml_read_state(XMLReadState &state, xml_node node) { - /* read shader */ - string shadername; - - if(xml_read_string(&shadername, node, "shader")) { - bool found = false; - - foreach(Shader *shader, state.scene->shaders) { - if(shader->name == shadername) { - state.shader = shader; - found = true; - break; - } - } - - if(!found) - fprintf(stderr, "Unknown shader \"%s\".\n", shadername.c_str()); - } - - xml_read_float(&state.dicing_rate, node, "dicing_rate"); - - /* read smooth/flat */ - if(xml_equal_string(node, "interpolation", "smooth")) - state.smooth = true; - else if(xml_equal_string(node, "interpolation", "flat")) - state.smooth = false; + /* read shader */ + string shadername; + + if (xml_read_string(&shadername, node, "shader")) { + bool found = false; + + foreach (Shader *shader, state.scene->shaders) { + if (shader->name == shadername) { + state.shader = shader; + found = true; + break; + } + } + + if (!found) + fprintf(stderr, "Unknown shader \"%s\".\n", shadername.c_str()); + } + + xml_read_float(&state.dicing_rate, node, "dicing_rate"); + + /* read smooth/flat */ + if (xml_equal_string(node, "interpolation", "smooth")) + state.smooth = true; + else if (xml_equal_string(node, "interpolation", "flat")) + state.smooth = false; } /* Scene */ -static void xml_read_include(XMLReadState& state, const string& src); +static void xml_read_include(XMLReadState &state, const string &src); -static void xml_read_scene(XMLReadState& state, xml_node scene_node) +static void xml_read_scene(XMLReadState &state, xml_node scene_node) { - for(xml_node node = scene_node.first_child(); node; node = node.next_sibling()) { - if(string_iequals(node.name(), "film")) { - xml_read_node(state, state.scene->film, node); - } - else if(string_iequals(node.name(), "integrator")) { - xml_read_node(state, state.scene->integrator, node); - } - else if(string_iequals(node.name(), "camera")) { - xml_read_camera(state, node); - } - else if(string_iequals(node.name(), "shader")) { - xml_read_shader(state, node); - } - else if(string_iequals(node.name(), "background")) { - xml_read_background(state, node); - } - else if(string_iequals(node.name(), "mesh")) { - xml_read_mesh(state, node); - } - else if(string_iequals(node.name(), "light")) { - xml_read_light(state, node); - } - else if(string_iequals(node.name(), "transform")) { - XMLReadState substate = state; - - xml_read_transform(node, substate.tfm); - xml_read_scene(substate, node); - } - else if(string_iequals(node.name(), "state")) { - XMLReadState substate = state; - - xml_read_state(substate, node); - xml_read_scene(substate, node); - } - else if(string_iequals(node.name(), "include")) { - string src; - - if(xml_read_string(&src, node, "src")) - xml_read_include(state, src); - } - else - fprintf(stderr, "Unknown node \"%s\".\n", node.name()); - } + for (xml_node node = scene_node.first_child(); node; node = node.next_sibling()) { + if (string_iequals(node.name(), "film")) { + xml_read_node(state, state.scene->film, node); + } + else if (string_iequals(node.name(), "integrator")) { + xml_read_node(state, state.scene->integrator, node); + } + else if (string_iequals(node.name(), "camera")) { + xml_read_camera(state, node); + } + else if (string_iequals(node.name(), "shader")) { + xml_read_shader(state, node); + } + else if (string_iequals(node.name(), "background")) { + xml_read_background(state, node); + } + else if (string_iequals(node.name(), "mesh")) { + xml_read_mesh(state, node); + } + else if (string_iequals(node.name(), "light")) { + xml_read_light(state, node); + } + else if (string_iequals(node.name(), "transform")) { + XMLReadState substate = state; + + xml_read_transform(node, substate.tfm); + xml_read_scene(substate, node); + } + else if (string_iequals(node.name(), "state")) { + XMLReadState substate = state; + + xml_read_state(substate, node); + xml_read_scene(substate, node); + } + else if (string_iequals(node.name(), "include")) { + string src; + + if (xml_read_string(&src, node, "src")) + xml_read_include(state, src); + } + else + fprintf(stderr, "Unknown node \"%s\".\n", node.name()); + } } /* Include */ -static void xml_read_include(XMLReadState& state, const string& src) +static void xml_read_include(XMLReadState &state, const string &src) { - /* open XML document */ - xml_document doc; - xml_parse_result parse_result; - - string path = path_join(state.base, src); - parse_result = doc.load_file(path.c_str()); - - if(parse_result) { - XMLReadState substate = state; - substate.base = path_dirname(path); - - xml_node cycles = doc.child("cycles"); - xml_read_scene(substate, cycles); - } - else { - fprintf(stderr, "%s read error: %s\n", src.c_str(), parse_result.description()); - exit(EXIT_FAILURE); - } + /* open XML document */ + xml_document doc; + xml_parse_result parse_result; + + string path = path_join(state.base, src); + parse_result = doc.load_file(path.c_str()); + + if (parse_result) { + XMLReadState substate = state; + substate.base = path_dirname(path); + + xml_node cycles = doc.child("cycles"); + xml_read_scene(substate, cycles); + } + else { + fprintf(stderr, "%s read error: %s\n", src.c_str(), parse_result.description()); + exit(EXIT_FAILURE); + } } /* File */ void xml_read_file(Scene *scene, const char *filepath) { - XMLReadState state; + XMLReadState state; - state.scene = scene; - state.tfm = transform_identity(); - state.shader = scene->default_surface; - state.smooth = false; - state.dicing_rate = 1.0f; - state.base = path_dirname(filepath); + state.scene = scene; + state.tfm = transform_identity(); + state.shader = scene->default_surface; + state.smooth = false; + state.dicing_rate = 1.0f; + state.base = path_dirname(filepath); - xml_read_include(state, path_filename(filepath)); + xml_read_include(state, path_filename(filepath)); - scene->params.bvh_type = SceneParams::BVH_STATIC; + scene->params.bvh_type = SceneParams::BVH_STATIC; } CCL_NAMESPACE_END diff --git a/intern/cycles/app/cycles_xml.h b/intern/cycles/app/cycles_xml.h index a7bc1895d4e..6a48980d8ea 100644 --- a/intern/cycles/app/cycles_xml.h +++ b/intern/cycles/app/cycles_xml.h @@ -29,4 +29,4 @@ void xml_read_file(Scene *scene, const char *filepath); CCL_NAMESPACE_END -#endif /* __CYCLES_XML_H__ */ +#endif /* __CYCLES_XML_H__ */ -- cgit v1.2.3