bool ProgramManager::compileShaderFromFile(const char *fileName, GLSLShader::shaderType type) { if (!this->fileExits(fileName)) { //cerr << "File Not Exist: " << fileName << endl; logString = "File Not Exist: " + string(fileName); return false; } const char *source = shaderSourceFromFile(fileName); return compileShaderFromString(source, type); }
const char* VideoShader::fragmentShader() const { DPTR_D(const VideoShader); // because we have to modify the shader, and shader source must be kept, so read the origin if (d.video_format.isPlanar()) { d.planar_frag = shaderSourceFromFile("shaders/planar.f.glsl"); } else { d.packed_frag = shaderSourceFromFile("shaders/rgb.f.glsl"); } QByteArray& frag = d.video_format.isPlanar() ? d.planar_frag : d.packed_frag; if (frag.isEmpty()) { qWarning("Empty fragment shader!"); return 0; } if (d.video_format.isPlanar() && d.video_format.bytesPerPixel(0) == 2) { if (d.video_format.isBigEndian()) frag.prepend("#define LA_16BITS_BE\n"); else frag.prepend("#define LA_16BITS_LE\n"); } return frag.constData(); }
const char* VideoShader::fragmentShader() const { DPTR_D(const VideoShader); // because we have to modify the shader, and shader source must be kept, so read the origin if (d.video_format.isPlanar()) { d.planar_frag = shaderSourceFromFile("shaders/planar.f.glsl"); } else { d.packed_frag = shaderSourceFromFile("shaders/rgb.f.glsl"); } QByteArray& frag = d.video_format.isPlanar() ? d.planar_frag : d.packed_frag; if (frag.isEmpty()) { qWarning("Empty fragment shader!"); return 0; } if (d.video_format.isRGB()) { frag.prepend("#define INPUT_RGB\n"); } else { // color space in glsl does not work unless we define 'YUV_MAT_GLSL'. we compute it in color matrix by cpu #if 0 switch (d.color_space) { case ColorTransform::BT601: frag.prepend("#define CS_BT601\n"); break; case ColorTransform::BT709: frag.prepend("#define CS_BT709\n"); default: break; } #endif if (d.video_format.isPlanar() && d.video_format.bytesPerPixel(0) == 2) { if (d.video_format.isBigEndian()) frag.prepend("#define YUV16BITS_BE_LUMINANCE_ALPHA\n"); else frag.prepend("#define YUV16BITS_LE_LUMINANCE_ALPHA\n"); } frag.prepend(QString("#define YUV%1P\n").arg(d.video_format.bitsPerPixel(0)).toUtf8()); } return frag.constData(); }
INTERNAL String shaderSourceFromFile(const String& filename) { String filePath = BaseDirectory::Shaders + filename; std::ifstream file; file.open(cString(filePath), std::ios::in | std::ios::binary); if (!file.is_open()) { panic("Failed to open shader file: " + filePath); return {}; } defer(file.close()); String output; String line; while (file.good()) { getline(file, line); line = Strings::trimSpace(line); if (Strings::hasPrefix(line, "#include")) { String includeFilename = substring(line, 8, len(line)); includeFilename = Strings::trimSpace(includeFilename); includeFilename = Strings::trim(includeFilename, "\""); if (len(includeFilename) > 0) { // Recursively append source of header file and append header // extension const String& withExt = cString(includeFilename + ".head.glsl"); append(output, shaderSourceFromFile(withExt)); } } else { append(output, line); } append(output, '\n'); // Append a *nix newline } return output; }
b32 ShaderProgram::attachShaderFromFile(ShaderType type, const String& filename) { const String source = shaderSourceFromFile(filename); return attachShaderFromMemory(type, source); }