void TranslatorGLSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) { if (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS) { InitBuiltInFunctionEmulatorForGLSLWorkarounds(emu, getShaderType()); } int targetGLSLVersion = ShaderOutputTypeToGLSLVersion(getOutputType()); InitBuiltInFunctionEmulatorForGLSLMissingFunctions(emu, getShaderType(), targetGLSLVersion); }
bool KShaderProgram::setShader(const KSharedResource &Shader){ auto shader = static_cast<const KShader *>(Shader.constGet()); if (shader->getShaderType() == ShaderType::VERTEX) { _kvert = Shader; } else if (shader->getShaderType() == ShaderType::FRAGMENT) { _kfrag = Shader; } else if (shader->getShaderType() == ShaderType::GEOMETRY) { _kgeom = Shader; } else { KD_PRINT("incorrect shader type."); return false; } return true; }
void TranslatorGLSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, ShCompileOptions compileOptions) { if (compileOptions & SH_EMULATE_ABS_INT_FUNCTION) { InitBuiltInAbsFunctionEmulatorForGLSLWorkarounds(emu, getShaderType()); } if (compileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) { InitBuiltInIsnanFunctionEmulatorForGLSLWorkarounds(emu, getShaderVersion()); } int targetGLSLVersion = ShaderOutputTypeToGLSLVersion(getOutputType()); InitBuiltInFunctionEmulatorForGLSLMissingFunctions(emu, getShaderType(), targetGLSLVersion); }
void TranslatorGLSL::translate(TIntermNode *root, int) { TInfoSinkBase& sink = getInfoSink().obj; // Write GLSL version. writeVersion(root); writePragma(); // Write extension behaviour as needed writeExtensionBehavior(); bool precisionEmulation = getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision; if (precisionEmulation) { EmulatePrecision emulatePrecision; root->traverse(&emulatePrecision); emulatePrecision.updateTree(); emulatePrecision.writeEmulationHelpers(sink, getOutputType()); } // Write emulated built-in functions if needed. if (!getBuiltInFunctionEmulator().IsOutputEmpty()) { sink << "// BEGIN: Generated code for built-in function emulation\n\n"; sink << "#define webgl_emu_precision\n\n"; getBuiltInFunctionEmulator().OutputEmulatedFunctions(sink); sink << "// END: Generated code for built-in function emulation\n\n"; } // Write array bounds clamping emulation if needed. getArrayBoundsClamper().OutputClampingFunctionDefinition(sink); // Declare gl_FragColor and glFragData as webgl_FragColor and webgl_FragData // if it's core profile shaders and they are used. if (getShaderType() == GL_FRAGMENT_SHADER && IsGLSL130OrNewer(getOutputType())) { TFragmentOutSearcher searcher; root->traverse(&searcher); ASSERT(!(searcher.usesGlFragData() && searcher.usesGlFragColor())); if (searcher.usesGlFragColor()) { sink << "out vec4 webgl_FragColor;\n"; } if (searcher.usesGlFragData()) { sink << "out vec4 webgl_FragData[gl_MaxDrawBuffers];\n"; } } // Write translated shader. TOutputGLSL outputGLSL(sink, getArrayIndexClampingStrategy(), getHashFunction(), getNameMap(), getSymbolTable(), getShaderVersion(), getOutputType()); root->traverse(&outputGLSL); }
Shader::Shader(ShaderType eType, std::string cFile) { m_iID = glCreateShader(eType); m_eType = eType; m_cType = getShaderType(eType); // load the file Files::Loader oFile(cFile); std::string cShader = oFile.read(); const char *cSource = cShader.c_str(); glShaderSource(m_iID, 1, &cSource, NULL); glCompileShader(m_iID); int iStatus; glGetShaderiv(m_iID, GL_COMPILE_STATUS, &iStatus); if (iStatus != GL_TRUE) { int iLength; glGetShaderiv(m_iID, GL_INFO_LOG_LENGTH, &iLength); char *cLog = new char[iLength]; glGetShaderInfoLog(m_iID, iLength, NULL, cLog); throwError(__FUNCTION__ + std::string(" shader failed, ") + cFile + std::string(", ") + getShaderType(eType)); SAFE_DELETE_ARRAY(cLog); glDeleteShader(m_iID); } }
GLuint ShaderProgram::compile(GLuint type, GLchar const *source) { GLuint shader = glCreateShader(type); const char* src[] = { "#version 400 core\n", texel_format_== FormatR ? "#define FORMAT_R\n" : texel_format_ == FormatRG ? "#define FORMAT_RG" : "", source }; glShaderSource(shader, 3, src, NULL); glCompileShader(shader); GLint compiled; glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); if (!compiled) { GLint length; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); std::string log(length, ' '); glGetShaderInfoLog(shader, length, &length, &log[0]); std::cerr << "Failed to compile shadertype: "+ getShaderType(type) << std::endl << log << std::endl; return false; } return shader; }
void TranslatorESSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) { if (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS) { InitBuiltInFunctionEmulatorForGLSLWorkarounds(emu, getShaderType()); } }
void TranslatorESSL::translate(TIntermNode *root, int) { TInfoSinkBase& sink = getInfoSink().obj; writePragma(); // Write built-in extension behaviors. writeExtensionBehavior(); bool precisionEmulation = getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision; if (precisionEmulation) { EmulatePrecision emulatePrecision; root->traverse(&emulatePrecision); emulatePrecision.updateTree(); emulatePrecision.writeEmulationHelpers(sink, SH_ESSL_OUTPUT); } // Write emulated built-in functions if needed. getBuiltInFunctionEmulator().OutputEmulatedFunctionDefinition( sink, getShaderType() == GL_FRAGMENT_SHADER); // Write array bounds clamping emulation if needed. getArrayBoundsClamper().OutputClampingFunctionDefinition(sink); // Write translated shader. TOutputESSL outputESSL(sink, getArrayIndexClampingStrategy(), getHashFunction(), getNameMap(), getSymbolTable(), getShaderVersion(), precisionEmulation); root->traverse(&outputESSL); }
void TranslatorGLSL::writeExtensionBehavior(TIntermNode *root) { TInfoSinkBase &sink = getInfoSink().obj; const TExtensionBehavior &extBehavior = getExtensionBehavior(); for (const auto &iter : extBehavior) { if (iter.second == EBhUndefined) { continue; } if (getOutputType() == SH_GLSL_COMPATIBILITY_OUTPUT) { // For GLSL output, we don't need to emit most extensions explicitly, // but some we need to translate in GL compatibility profile. if (iter.first == "GL_EXT_shader_texture_lod") { sink << "#extension GL_ARB_shader_texture_lod : " << getBehaviorString(iter.second) << "\n"; } if (iter.first == "GL_EXT_draw_buffers") { sink << "#extension GL_ARB_draw_buffers : " << getBehaviorString(iter.second) << "\n"; } } } // GLSL ES 3 explicit location qualifiers need to use an extension before GLSL 330 if (getShaderVersion() >= 300 && getOutputType() < SH_GLSL_330_CORE_OUTPUT && getShaderType() != GL_COMPUTE_SHADER) { sink << "#extension GL_ARB_explicit_attrib_location : require\n"; } // Need to enable gpu_shader5 to have index constant sampler array indexing if (getOutputType() != SH_ESSL_OUTPUT && getOutputType() < SH_GLSL_400_CORE_OUTPUT && getShaderVersion() == 100) { // Don't use "require" on to avoid breaking WebGL 1 on drivers that silently // support index constant sampler array indexing, but don't have the extension or // on drivers that don't have the extension at all as it would break WebGL 1 for // some users. sink << "#extension GL_ARB_gpu_shader5 : enable\n"; } TExtensionGLSL extensionGLSL(getOutputType()); root->traverse(&extensionGLSL); for (const auto &ext : extensionGLSL.getEnabledExtensions()) { sink << "#extension " << ext << " : enable\n"; } for (const auto &ext : extensionGLSL.getRequiredExtensions()) { sink << "#extension " << ext << " : require\n"; } }
GLuint ShaderProgram::AttachShader(GLuint shaderType, std::string source) { GLuint sh = compile(shaderType, source.c_str()); shader_programs_.push_back(sh); std::cout << "Attached shader of type: '" << getShaderType(shaderType) << "'\n"; return sh; }
void TranslatorESSL::translate(TIntermNode *root, int) { TInfoSinkBase& sink = getInfoSink().obj; int shaderVer = getShaderVersion(); if (shaderVer > 100) { sink << "#version " << shaderVer << " es\n"; } writePragma(); // Write built-in extension behaviors. writeExtensionBehavior(); bool precisionEmulation = getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision; if (precisionEmulation) { EmulatePrecision emulatePrecision(getSymbolTable(), shaderVer); root->traverse(&emulatePrecision); emulatePrecision.updateTree(); emulatePrecision.writeEmulationHelpers(sink, SH_ESSL_OUTPUT); } unsigned int temporaryIndex = 0; RecordConstantPrecision(root, &temporaryIndex); // Write emulated built-in functions if needed. if (!getBuiltInFunctionEmulator().IsOutputEmpty()) { sink << "// BEGIN: Generated code for built-in function emulation\n\n"; if (getShaderType() == GL_FRAGMENT_SHADER) { sink << "#if defined(GL_FRAGMENT_PRECISION_HIGH)\n" << "#define webgl_emu_precision highp\n" << "#else\n" << "#define webgl_emu_precision mediump\n" << "#endif\n\n"; } else { sink << "#define webgl_emu_precision highp\n"; } getBuiltInFunctionEmulator().OutputEmulatedFunctions(sink); sink << "// END: Generated code for built-in function emulation\n\n"; } // Write array bounds clamping emulation if needed. getArrayBoundsClamper().OutputClampingFunctionDefinition(sink); // Write translated shader. TOutputESSL outputESSL(sink, getArrayIndexClampingStrategy(), getHashFunction(), getNameMap(), getSymbolTable(), shaderVer, precisionEmulation); root->traverse(&outputESSL); }
void Shader::ShaderCode::makeShader() { if (!glIsShader(*_pos)) { *_pos = glCreateShader(getShaderType(_type)); //no Shader existent; creating } else { int type = 0; glGetShaderiv(*_pos, GL_SHADER_TYPE, &type); //if there is already a Shader, check if it has the right type if (type != getShaderType(_type)) { glDeleteShader(*_pos); //clear Shader if it is of the wrong Type (issues with Programs created from it?!) *_pos = glCreateShader(getShaderType(_type)); //recreate Shader of right Type } }//if there is a Shader created with the right type then it is going to be rewritten but not recreated should reduce memory if (_code.size()) { const char *c_str = _code.c_str(); glShaderSource(*_pos, 1, &c_str, NULL); } else LOG << "OpenGL Shader " << "Empty Source." << "\n"; int compiled = 0; glGetShaderiv(*_pos, GL_COMPILE_STATUS, &compiled); if (!compiled) glCompileShader(*_pos); glGetShaderiv(*_pos, GL_COMPILE_STATUS, &compiled); if (!compiled) { //not compiling GLint maxLength = 0; glGetShaderiv(*_pos, GL_INFO_LOG_LENGTH, &maxLength); if (maxLength > 1) { //The maxLength includes the NULL character vector<GLchar> infoLog = vector<GLchar>(maxLength); glGetShaderInfoLog(*_pos, maxLength, NULL, &infoLog[0]); LOG << "OpenGL Shader " << infoLog.data() << "\n"; } } }
void TranslatorGLSL::translate(TIntermNode* root) { TInfoSinkBase& sink = getInfoSink().obj; // Write GLSL version. writeVersion(getShaderType(), root, sink); // Write translated shader. TOutputGLSL outputGLSL(sink); root->traverse(&outputGLSL); }
void TranslatorHLSL::translate(TIntermNode *root, int compileOptions) { const ShBuiltInResources &resources = getResources(); int numRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1; sh::OutputHLSL outputHLSL(getShaderType(), getShaderVersion(), getExtensionBehavior(), getSourcePath(), getOutputType(), numRenderTargets, getUniforms(), compileOptions); outputHLSL.output(root, getInfoSink().obj); mInterfaceBlockRegisterMap = outputHLSL.getInterfaceBlockRegisterMap(); mUniformRegisterMap = outputHLSL.getUniformRegisterMap(); }
void TranslatorGLSL::writeVersion(TIntermNode *root) { TVersionGLSL versionGLSL(getShaderType(), getPragma(), getOutputType()); root->traverse(&versionGLSL); int version = versionGLSL.getVersion(); // We need to write version directive only if it is greater than 110. // If there is no version directive in the shader, 110 is implied. if (version > 110) { TInfoSinkBase& sink = getInfoSink().obj; sink << "#version " << version << "\n"; } }
void TranslatorESSL::translate(TIntermNode* root) { TInfoSinkBase& sink = getInfoSink().obj; // Write built-in extension behaviors. writeExtensionBehavior(); // Write emulated built-in functions if needed. getBuiltInFunctionEmulator().OutputEmulatedFunctionDefinition( sink, getShaderType() == GL_FRAGMENT_SHADER); // Write array bounds clamping emulation if needed. getArrayBoundsClamper().OutputClampingFunctionDefinition(sink); // Write translated shader. TOutputESSL outputESSL(sink, getArrayIndexClampingStrategy(), getHashFunction(), getNameMap(), getSymbolTable(), getShaderVersion()); root->traverse(&outputESSL); }
GLuint ShaderProgram::compile(GLuint type, GLchar const *source) { GLuint shader = glCreateShader(type); glShaderSource(shader, 1, &source, NULL); glCompileShader(shader); GLint compiled = 0; glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); if (compiled == GL_FALSE) { GLint logSize = 0; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize); std::string log(logSize, ' '); glGetShaderInfoLog(shader, logSize, &logSize, &log[0]); std::cerr << "Failed to compile shadertype: " << getShaderType(type) << std::endl << log << std::endl; glDeleteShader(shader); // Don't leak the shader. exit(EXIT_FAILURE); } return shader; }
void MainWindow::closeEvent(QCloseEvent *event) { QSettings settings; settings.setValue("Window/size", size()); settings.setValue("Window/position", pos()); settings.setValue("Window/state", saveState()); settings.setValue("3DView/ShowModelCenter", m_ui->actionShowModelCenter->isChecked()); settings.setValue("3DView/ShowNormals", m_ui->actionShowNormals->isChecked()); settings.setValue("3DView/ShowTangentAndBitangent", m_ui->actionShow_Tangent_And_Bitangent->isChecked()); settings.setValue("3DView/ShowAxes", m_ui->actionShowAxes->isChecked()); settings.setValue("3DView/ShowGrid", m_ui->actionShowGrid->isChecked()); settings.setValue("3DView/ShowLightSource", m_ui->actionShowLightSource->isChecked()); settings.setValue("3DView/LinkLightToCamera", m_ui->actionLink_Light_Source_To_Camera->isChecked()); settings.setValue("3DView/EnableUserShaders", m_actionEnableUserShaders->isChecked()); settings.setValue("3DView/Animate", m_ui->actionAnimate->isChecked()); settings.setValue("3DView/ShowConnectors", m_ui->actionShow_Connectors->isChecked()); settings.setValue("3DView/ShaderTag", wz_shader_type_tag[getShaderType()]); event->accept(); }
void CGLSLProgram::loadFromFile(const string filePath){ fstream shaderFile; string shaderLine, shaderCode; shaderFile.open(filePath, ios::in); if (shaderFile){ while (getline(shaderFile, shaderLine)) shaderCode.append(shaderLine + "\n"); compileShader(getShaderType(filePath), shaderCode); } else{ printf("The shader file doesnt exist !!\n"); } shaderFile.close(); }
void TranslatorGLSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) { int targetGLSLVersion = ShaderOutputTypeToGLSLVersion(getOutputType()); InitBuiltInFunctionEmulatorForGLSLMissingFunctions(emu, getShaderType(), targetGLSLVersion); }
void TranslatorGLSL::translate(TIntermNode *root, int compileOptions) { TInfoSinkBase& sink = getInfoSink().obj; // Write GLSL version. writeVersion(root); writePragma(); // Write extension behaviour as needed writeExtensionBehavior(root); bool precisionEmulation = getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision; if (precisionEmulation) { EmulatePrecision emulatePrecision(getSymbolTable(), getShaderVersion()); root->traverse(&emulatePrecision); emulatePrecision.updateTree(); emulatePrecision.writeEmulationHelpers(sink, getOutputType()); } // Write emulated built-in functions if needed. if (!getBuiltInFunctionEmulator().IsOutputEmpty()) { sink << "// BEGIN: Generated code for built-in function emulation\n\n"; sink << "#define webgl_emu_precision\n\n"; getBuiltInFunctionEmulator().OutputEmulatedFunctions(sink); sink << "// END: Generated code for built-in function emulation\n\n"; } // Write array bounds clamping emulation if needed. getArrayBoundsClamper().OutputClampingFunctionDefinition(sink); // Declare gl_FragColor and glFragData as webgl_FragColor and webgl_FragData // if it's core profile shaders and they are used. if (getShaderType() == GL_FRAGMENT_SHADER) { const bool mayHaveESSL1SecondaryOutputs = IsExtensionEnabled(getExtensionBehavior(), "GL_EXT_blend_func_extended") && getShaderVersion() == 100; const bool declareGLFragmentOutputs = IsGLSL130OrNewer(getOutputType()); bool hasGLFragColor = false; bool hasGLFragData = false; bool hasGLSecondaryFragColor = false; bool hasGLSecondaryFragData = false; for (const auto &outputVar : outputVariables) { if (declareGLFragmentOutputs) { if (outputVar.name == "gl_FragColor") { ASSERT(!hasGLFragColor); hasGLFragColor = true; continue; } else if (outputVar.name == "gl_FragData") { ASSERT(!hasGLFragData); hasGLFragData = true; continue; } } if (mayHaveESSL1SecondaryOutputs) { if (outputVar.name == "gl_SecondaryFragColorEXT") { ASSERT(!hasGLSecondaryFragColor); hasGLSecondaryFragColor = true; continue; } else if (outputVar.name == "gl_SecondaryFragDataEXT") { ASSERT(!hasGLSecondaryFragData); hasGLSecondaryFragData = true; continue; } } } ASSERT(!((hasGLFragColor || hasGLSecondaryFragColor) && (hasGLFragData || hasGLSecondaryFragData))); if (hasGLFragColor) { sink << "out vec4 webgl_FragColor;\n"; } if (hasGLFragData) { sink << "out vec4 webgl_FragData[gl_MaxDrawBuffers];\n"; } if (hasGLSecondaryFragColor) { sink << "out vec4 angle_SecondaryFragColor;\n"; } if (hasGLSecondaryFragData) { sink << "out vec4 angle_SecondaryFragData[" << getResources().MaxDualSourceDrawBuffers << "];\n"; } } // Write translated shader. TOutputGLSL outputGLSL(sink, getArrayIndexClampingStrategy(), getHashFunction(), getNameMap(), getSymbolTable(), getShaderVersion(), getOutputType()); root->traverse(&outputGLSL); }
GLuint Shader::loadShader(std::string path) { file = path; GLenum type = getShaderType(); std::string source = loadSourceCode(); return createAndCompileShader(type, source); }
void TranslatorHLSL::translate(TIntermNode *root, int compileOptions) { const ShBuiltInResources &resources = getResources(); int numRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1; sh::AddDefaultReturnStatements(root); SeparateDeclarations(root); // TODO (oetuaho): Sequence operators should also be split in case there is dynamic indexing of // a vector or matrix as an l-value inside (RemoveDynamicIndexing transformation step generates // statements in this case). SplitSequenceOperator(root, IntermNodePatternMatcher::kExpressionReturningArray | IntermNodePatternMatcher::kUnfoldedShortCircuitExpression | IntermNodePatternMatcher::kDynamicIndexingOfVectorOrMatrixInLValue, getTemporaryIndex(), getSymbolTable(), getShaderVersion()); // Note that SeparateDeclarations needs to be run before UnfoldShortCircuitToIf. UnfoldShortCircuitToIf(root, getTemporaryIndex()); SeparateExpressionsReturningArrays(root, getTemporaryIndex()); // Note that SeparateDeclarations needs to be run before SeparateArrayInitialization. SeparateArrayInitialization(root); // HLSL doesn't support arrays as return values, we'll need to make functions that have an array // as a return value to use an out parameter to transfer the array data instead. ArrayReturnValueToOutParameter(root, getTemporaryIndex()); if (!shouldRunLoopAndIndexingValidation(compileOptions)) { // HLSL doesn't support dynamic indexing of vectors and matrices. RemoveDynamicIndexing(root, getTemporaryIndex(), getSymbolTable(), getShaderVersion()); } // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which // use a vertex attribute as a condition, and some related computation in the else block. if (getOutputType() == SH_HLSL_3_0_OUTPUT && getShaderType() == GL_VERTEX_SHADER) { sh::RewriteElseBlocks(root, getTemporaryIndex()); } bool precisionEmulation = getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision; if (precisionEmulation) { EmulatePrecision emulatePrecision(getSymbolTable(), getShaderVersion()); root->traverse(&emulatePrecision); emulatePrecision.updateTree(); emulatePrecision.writeEmulationHelpers(getInfoSink().obj, getShaderVersion(), getOutputType()); } if ((compileOptions & SH_EXPAND_SELECT_HLSL_INTEGER_POW_EXPRESSIONS) != 0) { sh::ExpandIntegerPowExpressions(root, getTemporaryIndex()); } sh::OutputHLSL outputHLSL(getShaderType(), getShaderVersion(), getExtensionBehavior(), getSourcePath(), getOutputType(), numRenderTargets, getUniforms(), compileOptions); outputHLSL.output(root, getInfoSink().obj); mInterfaceBlockRegisterMap = outputHLSL.getInterfaceBlockRegisterMap(); mUniformRegisterMap = outputHLSL.getUniformRegisterMap(); }
void TranslatorESSL::translate(TIntermBlock *root, ShCompileOptions compileOptions, PerformanceDiagnostics * /*perfDiagnostics*/) { TInfoSinkBase &sink = getInfoSink().obj; int shaderVer = getShaderVersion(); if (shaderVer > 100) { sink << "#version " << shaderVer << " es\n"; } // Write built-in extension behaviors. writeExtensionBehavior(compileOptions); // Write pragmas after extensions because some drivers consider pragmas // like non-preprocessor tokens. writePragma(compileOptions); bool precisionEmulation = getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision; if (precisionEmulation) { EmulatePrecision emulatePrecision(&getSymbolTable()); root->traverse(&emulatePrecision); emulatePrecision.updateTree(); emulatePrecision.writeEmulationHelpers(sink, shaderVer, SH_ESSL_OUTPUT); } RecordConstantPrecision(root, &getSymbolTable()); // Write emulated built-in functions if needed. if (!getBuiltInFunctionEmulator().isOutputEmpty()) { sink << "// BEGIN: Generated code for built-in function emulation\n\n"; if (getShaderType() == GL_FRAGMENT_SHADER) { sink << "#if defined(GL_FRAGMENT_PRECISION_HIGH)\n" << "#define emu_precision highp\n" << "#else\n" << "#define emu_precision mediump\n" << "#endif\n\n"; } else { sink << "#define emu_precision highp\n"; } getBuiltInFunctionEmulator().outputEmulatedFunctions(sink); sink << "// END: Generated code for built-in function emulation\n\n"; } // Write array bounds clamping emulation if needed. getArrayBoundsClamper().OutputClampingFunctionDefinition(sink); if (getShaderType() == GL_COMPUTE_SHADER && isComputeShaderLocalSizeDeclared()) { const sh::WorkGroupSize &localSize = getComputeShaderLocalSize(); sink << "layout (local_size_x=" << localSize[0] << ", local_size_y=" << localSize[1] << ", local_size_z=" << localSize[2] << ") in;\n"; } if (getShaderType() == GL_GEOMETRY_SHADER_EXT) { WriteGeometryShaderLayoutQualifiers( sink, getGeometryShaderInputPrimitiveType(), getGeometryShaderInvocations(), getGeometryShaderOutputPrimitiveType(), getGeometryShaderMaxVertices()); } // Write translated shader. TOutputESSL outputESSL(sink, getArrayIndexClampingStrategy(), getHashFunction(), getNameMap(), &getSymbolTable(), getShaderType(), shaderVer, precisionEmulation, compileOptions); root->traverse(&outputESSL); }
void TranslatorESSL::writeExtensionBehavior(ShCompileOptions compileOptions) { TInfoSinkBase &sink = getInfoSink().obj; const TExtensionBehavior &extBehavior = getExtensionBehavior(); const bool isMultiviewExtEmulated = (compileOptions & (SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW | SH_SELECT_VIEW_IN_NV_GLSL_VERTEX_SHADER)) != 0u; for (TExtensionBehavior::const_iterator iter = extBehavior.begin(); iter != extBehavior.end(); ++iter) { if (iter->second != EBhUndefined) { const bool isMultiview = (iter->first == TExtension::OVR_multiview); if (getResources().NV_shader_framebuffer_fetch && iter->first == TExtension::EXT_shader_framebuffer_fetch) { sink << "#extension GL_NV_shader_framebuffer_fetch : " << GetBehaviorString(iter->second) << "\n"; } else if (getResources().NV_draw_buffers && iter->first == TExtension::EXT_draw_buffers) { sink << "#extension GL_NV_draw_buffers : " << GetBehaviorString(iter->second) << "\n"; } else if (isMultiview && isMultiviewExtEmulated) { if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_SELECT_VIEW_IN_NV_GLSL_VERTEX_SHADER) != 0u) { // Emit the NV_viewport_array2 extension in a vertex shader if the // SH_SELECT_VIEW_IN_NV_GLSL_VERTEX_SHADER option is set and the // OVR_multiview(2) extension is requested. sink << "#extension GL_NV_viewport_array2 : require\n"; } } else if (iter->first == TExtension::EXT_geometry_shader) { sink << "#ifdef GL_EXT_geometry_shader\n" << "#extension GL_EXT_geometry_shader : " << GetBehaviorString(iter->second) << "\n" << "#elif defined GL_OES_geometry_shader\n" << "#extension GL_OES_geometry_shader : " << GetBehaviorString(iter->second) << "\n"; if (iter->second == EBhRequire) { sink << "#else\n" << "#error \"No geometry shader extensions available.\" // Only generate " "this if the extension is \"required\"\n"; } sink << "#endif\n"; } else if (iter->first == TExtension::ANGLE_multi_draw) { // Don't emit anything. This extension is emulated ASSERT((compileOptions & SH_EMULATE_GL_DRAW_ID) != 0); continue; } else { sink << "#extension " << GetExtensionNameString(iter->first) << " : " << GetBehaviorString(iter->second) << "\n"; } } } }
void TranslatorGLSL::writeExtensionBehavior(TIntermNode *root, ShCompileOptions compileOptions) { TInfoSinkBase &sink = getInfoSink().obj; const TExtensionBehavior &extBehavior = getExtensionBehavior(); for (const auto &iter : extBehavior) { if (iter.second == EBhUndefined) { continue; } if (getOutputType() == SH_GLSL_COMPATIBILITY_OUTPUT) { // For GLSL output, we don't need to emit most extensions explicitly, // but some we need to translate in GL compatibility profile. if (iter.first == TExtension::EXT_shader_texture_lod) { sink << "#extension GL_ARB_shader_texture_lod : " << GetBehaviorString(iter.second) << "\n"; } if (iter.first == TExtension::EXT_draw_buffers) { sink << "#extension GL_ARB_draw_buffers : " << GetBehaviorString(iter.second) << "\n"; } if (iter.first == TExtension::EXT_geometry_shader) { sink << "#extension GL_ARB_geometry_shader4 : " << GetBehaviorString(iter.second) << "\n"; } } const bool isMultiview = (iter.first == TExtension::OVR_multiview); if (isMultiview && getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_SELECT_VIEW_IN_NV_GLSL_VERTEX_SHADER) != 0u) { // Emit the NV_viewport_array2 extension in a vertex shader if the // SH_SELECT_VIEW_IN_NV_GLSL_VERTEX_SHADER option is set and the OVR_multiview(2) // extension is requested. sink << "#extension GL_NV_viewport_array2 : require\n"; } } // GLSL ES 3 explicit location qualifiers need to use an extension before GLSL 330 if (getShaderVersion() >= 300 && getOutputType() < SH_GLSL_330_CORE_OUTPUT && getShaderType() != GL_COMPUTE_SHADER) { sink << "#extension GL_ARB_explicit_attrib_location : require\n"; } // Need to enable gpu_shader5 to have index constant sampler array indexing if (getOutputType() != SH_ESSL_OUTPUT && getOutputType() < SH_GLSL_400_CORE_OUTPUT && getShaderVersion() == 100) { // Don't use "require" on to avoid breaking WebGL 1 on drivers that silently // support index constant sampler array indexing, but don't have the extension or // on drivers that don't have the extension at all as it would break WebGL 1 for // some users. sink << "#extension GL_ARB_gpu_shader5 : enable\n"; } TExtensionGLSL extensionGLSL(getOutputType()); root->traverse(&extensionGLSL); for (const auto &ext : extensionGLSL.getEnabledExtensions()) { sink << "#extension " << ext << " : enable\n"; } for (const auto &ext : extensionGLSL.getRequiredExtensions()) { sink << "#extension " << ext << " : require\n"; } }
void TranslatorGLSL::translate(TIntermNode *root, ShCompileOptions compileOptions) { TInfoSinkBase& sink = getInfoSink().obj; // Write GLSL version. writeVersion(root); // Write extension behaviour as needed writeExtensionBehavior(root); // Write pragmas after extensions because some drivers consider pragmas // like non-preprocessor tokens. writePragma(compileOptions); // If flattening the global invariant pragma, write invariant declarations for built-in // variables. It should be harmless to do this twice in the case that the shader also explicitly // did this. However, it's important to emit invariant qualifiers only for those built-in // variables that are actually used, to avoid affecting the behavior of the shader. if ((compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) && getPragma().stdgl.invariantAll) { ASSERT(wereVariablesCollected()); switch (getShaderType()) { case GL_VERTEX_SHADER: sink << "invariant gl_Position;\n"; // gl_PointSize should be declared invariant in both ESSL 1.00 and 3.00 fragment // shaders if it's statically referenced. conditionallyOutputInvariantDeclaration("gl_PointSize"); break; case GL_FRAGMENT_SHADER: // The preprocessor will reject this pragma if it's used in ESSL 3.00 fragment // shaders, so we can use simple logic to determine whether to declare these // variables invariant. conditionallyOutputInvariantDeclaration("gl_FragCoord"); conditionallyOutputInvariantDeclaration("gl_PointCoord"); break; default: // Currently not reached, but leave this in for future expansion. ASSERT(false); break; } } if ((compileOptions & SH_REWRITE_TEXELFETCHOFFSET_TO_TEXELFETCH) != 0) { sh::RewriteTexelFetchOffset(root, getSymbolTable(), getShaderVersion()); } bool precisionEmulation = getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision; if (precisionEmulation) { EmulatePrecision emulatePrecision(getSymbolTable(), getShaderVersion()); root->traverse(&emulatePrecision); emulatePrecision.updateTree(); emulatePrecision.writeEmulationHelpers(sink, getShaderVersion(), getOutputType()); } // Write emulated built-in functions if needed. if (!getBuiltInFunctionEmulator().IsOutputEmpty()) { sink << "// BEGIN: Generated code for built-in function emulation\n\n"; sink << "#define webgl_emu_precision\n\n"; getBuiltInFunctionEmulator().OutputEmulatedFunctions(sink); sink << "// END: Generated code for built-in function emulation\n\n"; } // Write array bounds clamping emulation if needed. getArrayBoundsClamper().OutputClampingFunctionDefinition(sink); // Declare gl_FragColor and glFragData as webgl_FragColor and webgl_FragData // if it's core profile shaders and they are used. if (getShaderType() == GL_FRAGMENT_SHADER) { const bool mayHaveESSL1SecondaryOutputs = IsExtensionEnabled(getExtensionBehavior(), "GL_EXT_blend_func_extended") && getShaderVersion() == 100; const bool declareGLFragmentOutputs = IsGLSL130OrNewer(getOutputType()); bool hasGLFragColor = false; bool hasGLFragData = false; bool hasGLSecondaryFragColor = false; bool hasGLSecondaryFragData = false; for (const auto &outputVar : outputVariables) { if (declareGLFragmentOutputs) { if (outputVar.name == "gl_FragColor") { ASSERT(!hasGLFragColor); hasGLFragColor = true; continue; } else if (outputVar.name == "gl_FragData") { ASSERT(!hasGLFragData); hasGLFragData = true; continue; } } if (mayHaveESSL1SecondaryOutputs) { if (outputVar.name == "gl_SecondaryFragColorEXT") { ASSERT(!hasGLSecondaryFragColor); hasGLSecondaryFragColor = true; continue; } else if (outputVar.name == "gl_SecondaryFragDataEXT") { ASSERT(!hasGLSecondaryFragData); hasGLSecondaryFragData = true; continue; } } } ASSERT(!((hasGLFragColor || hasGLSecondaryFragColor) && (hasGLFragData || hasGLSecondaryFragData))); if (hasGLFragColor) { sink << "out vec4 webgl_FragColor;\n"; } if (hasGLFragData) { sink << "out vec4 webgl_FragData[gl_MaxDrawBuffers];\n"; } if (hasGLSecondaryFragColor) { sink << "out vec4 angle_SecondaryFragColor;\n"; } if (hasGLSecondaryFragData) { sink << "out vec4 angle_SecondaryFragData[" << getResources().MaxDualSourceDrawBuffers << "];\n"; } } if (getShaderType() == GL_COMPUTE_SHADER && isComputeShaderLocalSizeDeclared()) { const sh::WorkGroupSize &localSize = getComputeShaderLocalSize(); sink << "layout (local_size_x=" << localSize[0] << ", local_size_y=" << localSize[1] << ", local_size_z=" << localSize[2] << ") in;\n"; } // Write translated shader. TOutputGLSL outputGLSL(sink, getArrayIndexClampingStrategy(), getHashFunction(), getNameMap(), getSymbolTable(), getShaderType(), getShaderVersion(), getOutputType(), compileOptions); root->traverse(&outputGLSL); }
MStatus TestLightNode::initialize() { MFnTypedAttribute tAttr; MFnStringData tDefault; MFnNumericAttribute nAttr; MFnEnumAttribute eAttr; MFnLightDataAttribute lAttr; MStatus status; MObject string; // Create input attributes aRmanShader = tAttr.create( MString("rmanShader"), MString("rms"), MFnData::kString, tDefault.create(getTypeName()), &status ); MAKE_INPUT(tAttr); aRmanShaderType = tAttr.create( MString("rmanShaderType"), MString("rst"), MFnData::kString, tDefault.create(getShaderType()), &status ); MAKE_INPUT(tAttr); aRmanShaderLong = tAttr.create( MString("rmanShaderLong"), MString("rml"), MFnData::kString, aRmanShaderLong, &status ); MAKE_INPUT(tAttr); aRmanShaderLif = tAttr.create( MString("rmanShaderLif"), MString("lif"), MFnData::kString, aRmanShaderLif, &status ); MAKE_INPUT(tAttr); aRmanParams = tAttr.create( MString("rmanParams"), MString("rpr"), MFnData::kStringArray, aRmanParams, &status ); MAKE_INPUT(tAttr); aRmanDetails = tAttr.create( MString("rmanDetails"), MString("rdt"), MFnData::kStringArray, aRmanDetails, &status ); MAKE_INPUT(tAttr); aRmanTypes = tAttr.create( MString("rmanTypes"), MString("rty"), MFnData::kStringArray, aRmanTypes, &status ); MAKE_INPUT(tAttr); aRmanDefaults = tAttr.create( MString("rmanDefaults"), MString("rdf"), MFnData::kStringArray, aRmanDefaults, &status ); MAKE_INPUT(tAttr); aRmanArraySizes = tAttr.create( MString("rmanArraySizes"), MString("ras"), MFnData::kIntArray, aRmanArraySizes, &status ); MAKE_INPUT(tAttr); aRmanLifCmds = tAttr.create( MString("rmanLifCmds"), MString("rlc"), MFnData::kStringArray, aRmanLifCmds, &status ); MAKE_INPUT(tAttr); aRmanMethods = tAttr.create( MString("rmanMethods"), MString("rmt"), MFnData::kStringArray, aRmanMethods, &status ); MAKE_INPUT(tAttr); aRmanIsOutput = tAttr.create( MString("rmanIsOutput"), MString("rio"), MFnData::kIntArray, aRmanIsOutput, &status ); MAKE_INPUT(tAttr); aRmanAccept = tAttr.create( MString("rmanAccept"), MString("rma"), MFnData::kStringArray, aRmanAccept, &status ); MAKE_INPUT(tAttr); aOutColor = nAttr.createColor("outColor", "oc"); MAKE_OUTPUT(nAttr); aOutTransparency = nAttr.createColor("outTransparency", "ot"); MAKE_OUTPUT(nAttr); IfMErrorWarn( addAttribute( aRmanShader ) ); IfMErrorWarn( addAttribute( aRmanShaderType ) ); IfMErrorWarn( addAttribute( aRmanShaderLong ) ); IfMErrorWarn( addAttribute( aRmanShaderLif ) ); IfMErrorWarn( addAttribute( aRmanParams ) ); IfMErrorWarn( addAttribute( aRmanDetails ) ); IfMErrorWarn( addAttribute( aRmanTypes ) ); IfMErrorWarn( addAttribute( aRmanDefaults ) ); IfMErrorWarn( addAttribute( aRmanArraySizes ) ); IfMErrorWarn( addAttribute( aRmanLifCmds ) ); IfMErrorWarn( addAttribute( aRmanMethods) ); IfMErrorWarn( addAttribute( aRmanIsOutput) ); IfMErrorWarn( addAttribute( aRmanAccept) ); IfMErrorWarn( addAttribute( aOutColor ) ); IfMErrorWarn( addAttribute( aOutTransparency ) ); initialize_shader_parameters(); return MS::kSuccess; }
void MainWindow::actionReloadUserShader() { wz_shader_type_t type = getShaderType(); shaderAction(type); }
void CGLSLProgram::loadFromString(const string fileName, const string shaderCode){ compileShader(getShaderType(fileName), shaderCode); }