//------------------------------------------------------------------------------ static void callbackKernel(int k) { g_kernel = k; #ifdef OPENSUBDIV_HAS_OPENCL if (g_kernel == kCL and (not g_clDeviceContext.IsInitialized())) { if (g_clDeviceContext.Initialize() == false) { printf("Error in initializing OpenCL\n"); exit(1); } } #endif #ifdef OPENSUBDIV_HAS_CUDA if (g_kernel == kCUDA and (not g_cudaDeviceContext.IsInitialized())) { if (g_cudaDeviceContext.Initialize() == false) { printf("Error in initializing Cuda\n"); exit(1); } } #endif createOsdMesh(g_defaultShapes[g_currentShape], g_level); }
//------------------------------------------------------------------------------ static void callbackKernel(int k) { g_kernel = k; #ifdef OPENSUBDIV_HAS_OPENCL if (g_kernel == kCL and (not g_clDeviceContext.IsInitialized())) { if (g_clDeviceContext.Initialize() == false) { printf("Error in initializing OpenCL\n"); exit(1); } } #endif #ifdef OPENSUBDIV_HAS_CUDA if (g_kernel == kCUDA and (not g_cudaDeviceContext.IsInitialized())) { if (g_cudaDeviceContext.Initialize() == false) { printf("Error in initializing Cuda\n"); exit(1); } } #endif rebuildMesh(); }
int main(int argc, char ** argv) { int width = 128; int height = 128; int tessLevel = 1; int isolationLevel = 2; bool writeToFile = false; bool adaptive = false; std::string prefix; std::string displayMode = "PATCH_TYPE"; std::vector<std::string> kernels; for (int i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-a")) { adaptive = true; } else if (!strcmp(argv[i], "-l")) { isolationLevel = atoi(argv[++i]); } else if (!strcmp(argv[i], "-k")) { std::stringstream ss(argv[++i]); std::string kernel; while(std::getline(ss, kernel, ',')) { kernels.push_back(kernel); } } else if (!strcmp(argv[i], "-t")) { tessLevel = atoi(argv[++i]); } else if (!strcmp(argv[i], "-w")) { writeToFile = true; prefix = argv[++i]; } else if (!strcmp(argv[i], "-s")) { width = atoi(argv[++i]); height = atoi(argv[++i]); } else if (!strcmp(argv[i], "-d")) { displayMode = argv[++i]; } else { usage(argv[0]); return 1; } } // by default, test all available kernels if (kernels.empty()) { kernels.push_back("CPU"); #ifdef OPENSUBDIV_HAS_OPENMP kernels.push_back("OPENMP"); #endif #ifdef OPENSUBDIV_HAS_TBB kernels.push_back("TBB"); #endif #ifdef OPENSUBDIV_HAS_CUDA kernels.push_back("CUDA"); #endif #ifdef OPENSUBDIV_HAS_OPENCL kernels.push_back("CL"); #endif #ifdef OPENSUBDIV_HAS_GLSL_TRANSFORM_FEEDBACK kernels.push_back("XFB"); #endif #ifdef OPENSUBDIV_HAS_GLSL_COMPUTE kernels.push_back("GLSL"); #endif } if (! glfwInit()) { std::cout << "Failed to initialize GLFW\n"; return 1; } static const char windowTitle[] = "OpenSubdiv imaging test " OPENSUBDIV_VERSION_STRING; GLUtils::SetMinimumGLVersion(); GLFWwindow *window = glfwCreateWindow(width, height, windowTitle, NULL, NULL); if (! window) { std::cerr << "Failed to create OpenGL context.\n"; glfwTerminate(); } glfwMakeContextCurrent(window); GLUtils::PrintGLVersion(); #if defined(OSD_USES_GLEW) // this is the only way to initialize glew correctly under core profile context. glewExperimental = true; if (GLenum r = glewInit() != GLEW_OK) { std::cout << "Failed to initialize glew. Error = " << glewGetErrorString(r) << "\n"; exit(1); } // clear GL errors generated during glewInit() glGetError(); #endif initShapes(); // initialize GL states glViewport(0, 0, width, height); glEnable(GL_DEPTH_TEST); // some regression shapes are not visible in this camera // with backface culling. // glEnable(GL_CULL_FACE); // transform uniform float modelview[16] = { 0.945518f, -0.191364f, 0.263390f, 0.000000f, 0.325568f, 0.555762f, -0.764941f, 0.000000f, 0.000000f, 0.809017f, 0.587785f, 0.000000f, 0.000000f, 0.000000f, -1.500000f, 1.000000f }; float projection[16] = { 2.414213f, 0.000000f, 0.000000f, 0.000000f, 0.000000f, 2.414213f, 0.000000f, 0.000000f, 0.000000f, 0.000000f, -1.000000f, -1.000000f, 0.000000f, 0.000000f, -0.000200f, 0.000000f }; struct Transform { float ModelViewMatrix[16]; float ProjectionMatrix[16]; float Viewport[4]; float TessLevel; } transformData; memcpy(transformData.ModelViewMatrix, modelview, sizeof(modelview)); memcpy(transformData.ProjectionMatrix, projection, sizeof(projection)); transformData.Viewport[0] = 0; transformData.Viewport[1] = 0; transformData.Viewport[2] = static_cast<float>(width); transformData.Viewport[3] = static_cast<float>(height); transformData.TessLevel = static_cast<float>(1 << tessLevel); GLuint transformUB = 0; glGenBuffers(1, &transformUB); glBindBuffer(GL_UNIFORM_BUFFER, transformUB); glBufferData(GL_UNIFORM_BUFFER, sizeof(transformData), &transformData, GL_STATIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, /*binding=*/0, transformUB); // create draw registry; ShaderCache shaderCache(displayMode); // write report html if (writeToFile) { std::string reportfile = prefix + ".html"; std::ofstream ofs(reportfile.c_str()); ofs << "<html>\n" << "<head><style>\n" << "table { border-collapse:collapse; } " << "table,th,td {border: 1px solid black} " << "</style></head>\n"; ofs << "<body>\n"; ofs << "<h3>OpenSubdiv imaging regression test<h3>\n"; ofs << "<pre>\n"; ofs << "OpenSubdiv : " << OPENSUBDIV_VERSION_STRING << "\n"; ofs << "GL Version : " << glGetString(GL_VERSION) << ", " << glGetString(GL_VENDOR) << ", " << glGetString(GL_RENDERER) << "\n"; ofs << "Isolation Level : " << isolationLevel << "\n"; ofs << "Tess Level : " << tessLevel << "\n"; ofs << "Adaptive : " << adaptive << "\n"; ofs << "Display Mode : " << displayMode << "\n"; ofs << "</pre>\n"; ofs << "<table>\n"; ofs << "<tr>\n"; ofs << "<th>Reference<br>(on github. to be updated)</th>\n"; for (size_t k = 0; k < kernels.size(); ++k) { ofs << "<th>" << kernels[k] << "</th>\n"; } ofs << "</tr>\n"; for (size_t i = 0; i < g_shapes.size(); ++i) { ofs << "<tr>\n"; ofs << "<td>" << g_shapes[i].name << "</td>\n"; for (size_t k = 0; k < kernels.size(); ++k) { ofs << "<td>"; ofs << "<img src='" << prefix << "_" << kernels[k] << "_" << g_shapes[i].name << ".png'>"; ofs << "</td>"; } ofs << "</tr>\n"; } ofs << "</table>\n"; ofs << "</body></html>\n"; ofs.close(); } // run test for (size_t k = 0; k < kernels.size(); ++k) { std::string const &kernel = kernels[k]; // prep GPU kernel #ifdef OPENSUBDIV_HAS_OPENCL if (kernel == "CL" && CLDeviceContext::HAS_CL_VERSION_1_1()) { if (g_clDeviceContext.IsInitialized() == false) { if (g_clDeviceContext.Initialize() == false) { std::cout << "Error in initializing OpenCL\n"; exit(1); } } } #endif #ifdef OPENSUBDIV_HAS_CUDA if (kernel == "CUDA") { if (g_cudaDeviceContext.IsInitialized() == false) { if (g_cudaDeviceContext.Initialize() == false) { std::cout << "Error in initializing Cuda\n"; exit(1); } } } #endif for (size_t i = 0; i < g_shapes.size(); ++i) { // run test runTest(g_shapes[i], kernel, isolationLevel, adaptive, &shaderCache); if (writeToFile) { // read back pixels std::vector<unsigned char> data(width*height*3); glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, &data[0]); // write image std::string filename = prefix + "_" + kernel + "_" + g_shapes[i].name + ".png"; // flip vertical stbi_write_png(filename.c_str(), width, height, 3, &data[width*3*(height-1)], -width*3); } glfwSwapBuffers(window); } } return 0; }