GLuint compileShader(GLenum type, const char *source) { /* create shader object */ GLuint shader = glCreateShader(type); if (shader == 0) { std::cerr << "Failed to create shader object: " << errorToStr(glGetError()) << std::endl; return 0; } /* copy the sources to the shader object */ glShaderSource(shader, 1, &source, NULL); /* compile the sources */ glCompileShader(shader); /* query compilation status */ GLint status = GL_FALSE; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); /* print compilation log */ std::cerr << "Compilation status: " << (status == GL_TRUE ? "succeeded" : "failed") << std::endl; std::cerr << getShaderInfoLog(shader) << std::endl; /* check compilation status */ if (status == GL_FALSE) { glDeleteShader(shader); return 0; } return shader; }
bool SocketBase::error(const char* msg, int err) { if (!isValid()) return false; disconnect(); LOG(0, "*** [Socket] %s: %s (%d)\n", msg, errorToStr(err), err); return false; }
bool ShaderProgram::attachShader(GLenum type, const char *source, GLuint *shader_id) { assert(glIsProgram(m_program)); /* create shader object */ GLuint shader = glCreateShader(type); if (shader == 0) { ERROR("Failed to create shader object: " << errorToStr(glGetError())); return false; } /* copy the sources to the shader object */ glShaderSource(shader, 1, &source, NULL); /* compile the sources */ glCompileShader(shader); /* query compilation status */ GLint status = GL_FALSE; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); /* print compilation log */ std::cerr << "Compilation status: " << (status == GL_TRUE ? "succeeded" : "failed") << std::endl; std::cerr << getShaderInfoLog(shader) << std::endl; /* check compilation status */ if (status == GL_FALSE) { glDeleteShader(shader); return false; } /* attach the shader to program */ glAttachShader(m_program, shader); if (shader_id == nullptr) { glDeleteShader(shader); } else { *shader_id = shader; } return true; }
GLuint linkProgram(unsigned int count, ...) { /* create program object */ GLuint program = glCreateProgram(); if (program == 0) { std::cerr << "Failed to create shader program object: " << errorToStr(glGetError()) << std::endl; return 0; } /* attach shaders */ va_list args; va_start(args, count); for (unsigned int i = 0; i < count; ++i) { glAttachShader(program, va_arg(args, GLuint)); } va_end(args); /* link program */ glLinkProgram(program); /* query link status */ GLint status = GL_FALSE; glGetProgramiv(program, GL_LINK_STATUS, &status); /* print linking log */ std::cerr << "Linking status: " << (status == GL_TRUE ? "succeeded" : "failed") << std::endl; std::cerr << getProgramInfoLog(program) << std::endl; /* check linking status */ if (status == GL_FALSE) { glDeleteProgram(program); return 0; } return program; }
bool Texture::load(GLsizei w, GLsizei h, const GLvoid *pixels, GLint src_format, GLenum src_type, GLenum dest_format, bool mipmapped) { //assert(glIsTexture(m_id)); assert(pixels != nullptr); glBindTexture(GL_TEXTURE_2D, m_id); glTexImage2D(GL_TEXTURE_2D, 0, dest_format, w, h, 0, src_format, src_type, pixels); if (mipmapped) { glGenerateMipmap(GL_TEXTURE_2D); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } GLenum err = glGetError(); glBindTexture(GL_TEXTURE_2D, 0); if (err != GL_NO_ERROR) { ERROR("Failed to store texture data on GPU: " << errorToStr(err)); return false; } return true; }
bool selectGLDeviceAndPlatform(cl_device_id *device, cl_platform_id *platform) { assert(device != nullptr); assert(platform != nullptr); /* first try to get the necessary extension */ clGetGLContextInfoKHR_fn clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddress("clGetGLContextInfoKHR"); if (clGetGLContextInfoKHR == nullptr) { ERRORM("clGetGLContextInfoKHR extension function not supproted."); return false; } #if defined(FLUIDSIM_OS_MAC) cl_context_properties props[] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties) CGLGetShareGroup(CGLGetCurrentContext()), 0 }; cl_int err = clGetGLContextInfoKHR(props, // the OpenGL context CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, // get the id of the device currently executing OpenGL sizeof(*device), device, nullptr); if (err != CL_SUCCESS) { ERRORM("Failed to retrieve the OpenCL id of the device executing OpenGL: " << errorToStr(err)); return false; } /* get the platform associated with the device */ err = clGetDeviceInfo(*device, CL_DEVICE_PLATFORM, sizeof(*platform), platform, nullptr); if (err != CL_SUCCESS) { ERRORM("Failed to retirieve platform id for the device executing OpenGL: " << errorToStr(err)); return false; } #else cl_context_properties props[] = { #if defined(FLUIDSIM_OS_UNIX) CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(), CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(), CL_CONTEXT_PLATFORM, (cl_context_properties) nullptr, #elif defined(FLUIDSIM_OS_WIN) CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(), CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(), CL_CONTEXT_PLATFORM, (cl_context_properties) nullptr, #else # error "Unsupported OS platform" #endif 0 }; std::vector<boost::compute::platform> platform_list = boost::compute::system::platforms(); for (const boost::compute::platform & p : platform_list) { WARNM("platform: " << p.name()); props[5] = (cl_context_properties) p.id(); cl_int err = clGetGLContextInfoKHR(props, // the OpenGL context CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, // get the id of the device currently executing OpenGL sizeof(*device), device, nullptr); if ((err == CL_SUCCESS) && (boost::compute::device(*device).type() == CL_DEVICE_TYPE_GPU)) { *platform = (cl_platform_id) props[5]; return true; } else { WARNM("clGetGLContextInfoKHR: " << errorToStr(err)); } } #endif return false; }