- return value;
-}
-
-///\todo BlendFunc parsing
-BlendFunc evaluateBlendFunc(const BlendFuncExpression& blendFunc, const ShaderParameters& params, const ShaderArguments& args)
-{
- return BlendFunc(BLEND_ONE, BLEND_ZERO);
-}
-
-qtexture_t* evaluateTexture(const TextureExpression& texture, const ShaderParameters& params, const ShaderArguments& args, const LoadImageCallback& loader = GlobalTexturesCache().defaultLoader())
-{
- StringOutputStream result(64);
- const char* expression = texture.c_str();
- const char* end = expression + string_length(expression);
- if(!string_empty(expression))
- {
- for(;;)
- {
- const char* best = end;
- const char* bestParam = 0;
- const char* bestArg = 0;
- ShaderArguments::const_iterator j = args.begin();
- for(ShaderParameters::const_iterator i = params.begin(); i != params.end(); ++i, ++j)
- {
- const char* found = strstr(expression, (*i).c_str());
- if(found != 0 && found < best)
- {
- best = found;
- bestParam = (*i).c_str();
- bestArg = (*j).c_str();
- }
- }
- if(best != end)
- {
- result << StringRange(expression, best);
- result << PathCleaned(bestArg);
- expression = best + string_length(bestParam);
- }
- else
- {
- break;
- }
- }
- result << expression;
- }
- return GlobalTexturesCache().capture(loader, result.c_str());
-}
-
-float evaluateFloat(const ShaderValue& value, const ShaderParameters& params, const ShaderArguments& args)
-{
- const char* result = evaluateShaderValue(value.c_str(), params, args);
- float f;
- if(!string_parse_float(result, f))
- {
- globalErrorStream() << "parsing float value failed: " << makeQuoted(result) << "\n";
- }
- return f;
-}
-
-BlendFactor evaluateBlendFactor(const ShaderValue& value, const ShaderParameters& params, const ShaderArguments& args)
-{
- const char* result = evaluateShaderValue(value.c_str(), params, args);
-
- if(string_equal_nocase(result, "gl_zero"))
- {
- return BLEND_ZERO;
- }
- if(string_equal_nocase(result, "gl_one"))
- {
- return BLEND_ONE;
- }
- if(string_equal_nocase(result, "gl_src_color"))
- {
- return BLEND_SRC_COLOUR;
- }
- if(string_equal_nocase(result, "gl_one_minus_src_color"))
- {
- return BLEND_ONE_MINUS_SRC_COLOUR;
- }
- if(string_equal_nocase(result, "gl_src_alpha"))
- {
- return BLEND_SRC_ALPHA;
- }
- if(string_equal_nocase(result, "gl_one_minus_src_alpha"))
- {
- return BLEND_ONE_MINUS_SRC_ALPHA;
- }
- if(string_equal_nocase(result, "gl_dst_color"))
- {
- return BLEND_DST_COLOUR;
- }
- if(string_equal_nocase(result, "gl_one_minus_dst_color"))
- {
- return BLEND_ONE_MINUS_DST_COLOUR;
- }
- if(string_equal_nocase(result, "gl_dst_alpha"))
- {
- return BLEND_DST_ALPHA;
- }
- if(string_equal_nocase(result, "gl_one_minus_dst_alpha"))
- {
- return BLEND_ONE_MINUS_DST_ALPHA;
- }
- if(string_equal_nocase(result, "gl_src_alpha_saturate"))
- {
- return BLEND_SRC_ALPHA_SATURATE;
- }
-
- globalErrorStream() << "parsing blend-factor value failed: " << makeQuoted(result) << "\n";
- return BLEND_ZERO;
-}
-
-class CShader : public IShader
-{
- std::size_t m_refcount;
-
- const ShaderTemplate& m_template;
- const ShaderArguments& m_args;
- const char* m_filename;
- // name is shader-name, otherwise texture-name (if not a real shader)
- CopiedString m_Name;
-
- qtexture_t* m_pTexture;
- qtexture_t* m_notfound;
- qtexture_t* m_pDiffuse;
- float m_heightmapScale;
- qtexture_t* m_pBump;
- qtexture_t* m_pSpecular;
- qtexture_t* m_pLightFalloffImage;
- BlendFunc m_blendFunc;
-
- bool m_bInUse;
-
-
-public:
- static bool m_lightingEnabled;
-
- CShader(const ShaderDefinition& definition) :
- m_refcount(0),
- m_template(*definition.shaderTemplate),
- m_args(definition.args),
- m_filename(definition.filename),
- m_blendFunc(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA),
- m_bInUse(false)
- {
- m_pTexture = 0;
- m_pDiffuse = 0;
- m_pBump = 0;
- m_pSpecular = 0;
-
- m_notfound = 0;
-
- realise();
- }
- virtual ~CShader()
- {
- unrealise();
-
- ASSERT_MESSAGE(m_refcount == 0, "deleting active shader");
- }
-
- // IShaders implementation -----------------
- void IncRef()
- {
- ++m_refcount;
- }
- void DecRef()
- {
- ASSERT_MESSAGE(m_refcount != 0, "shader reference-count going below zero");
- if(--m_refcount == 0)
- {
- delete this;
- }
- }
-
- std::size_t refcount()
- {
- return m_refcount;
- }
-
- // get/set the qtexture_t* Radiant uses to represent this shader object
- qtexture_t* getTexture() const
- {
- return m_pTexture;
- }
- qtexture_t* getDiffuse() const
- {
- return m_pDiffuse;
- }
- qtexture_t* getBump() const
- {
- return m_pBump;
- }
- qtexture_t* getSpecular() const
- {
- return m_pSpecular;
- }
- // get shader name
- const char* getName() const
- {
- return m_Name.c_str();
- }
- bool IsInUse() const
- {
- return m_bInUse;
- }
- void SetInUse(bool bInUse)
- {
- m_bInUse = bInUse;
- g_ActiveShadersChangedNotify();
- }
- // get the shader flags
- int getFlags() const
- {
- return m_template.m_nFlags;
- }
- // get the transparency value
- float getTrans() const
- {
- return m_template.m_fTrans;
- }
- // test if it's a true shader, or a default shader created to wrap around a texture
- bool IsDefault() const
- {
- return string_empty(m_filename);
- }
- // get the alphaFunc
- void getAlphaFunc(EAlphaFunc *func, float *ref) { *func = m_template.m_AlphaFunc; *ref = m_template.m_AlphaRef; };
- BlendFunc getBlendFunc() const
- {
- return m_blendFunc;
- }
- // get the cull type
- ECull getCull()
- {
- return m_template.m_Cull;
- };
- // get shader file name (ie the file where this one is defined)
- const char* getShaderFileName() const
- {
- return m_filename;
- }
- // -----------------------------------------
-
- void realise()
- {
- m_pTexture = evaluateTexture(m_template.m_textureName, m_template.m_params, m_args);
-
- if(m_pTexture->texture_number == 0)
- {
- m_notfound = m_pTexture;
-
- {
- StringOutputStream name(256);
- name << GlobalRadiant().getAppPath() << "bitmaps/" << (IsDefault() ? "notex.bmp" : "shadernotex.bmp");
- m_pTexture = GlobalTexturesCache().capture(LoadImageCallback(0, loadBitmap), name.c_str());
- }
- }
-
- realiseLighting();
-
- if(m_layers.size() == 1)
- {
- const BlendFuncExpression& blendFunc = m_template.m_layers.front().blendFunc();
- if(!string_empty(blendFunc.second.c_str()))
- {
- m_blendFunc = BlendFunc(
- evaluateBlendFactor(blendFunc.first.c_str(), m_template.m_params, m_args),
- evaluateBlendFactor(blendFunc.second.c_str(), m_template.m_params, m_args)
- );
- }
- else
- {
- const char* blend = evaluateShaderValue(blendFunc.first.c_str(), m_template.m_params, m_args);
-
- if(string_equal_nocase(blend, "add"))
- {
- m_blendFunc = BlendFunc(BLEND_ONE, BLEND_ONE);
- }
- else if(string_equal_nocase(blend, "filter"))
- {
- m_blendFunc = BlendFunc(BLEND_DST_COLOUR, BLEND_ZERO);
- }
- else if(string_equal_nocase(blend, "blend"))
- {
- m_blendFunc = BlendFunc(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
- }
- else
- {
- globalErrorStream() << "parsing blend value failed: " << makeQuoted(blend) << "\n";
- }
- }
- }
- }
-
- void unrealise()
- {
- GlobalTexturesCache().release(m_pTexture);
-
- if(m_notfound != 0)
- {
- GlobalTexturesCache().release(m_notfound);
- }
-
- unrealiseLighting();
- }
-
- void realiseLighting()
- {
- if(m_lightingEnabled)
- {
- LoadImageCallback loader = GlobalTexturesCache().defaultLoader();
- if(!string_empty(m_template.m_heightmapScale.c_str()))
- {
- m_heightmapScale = evaluateFloat(m_template.m_heightmapScale, m_template.m_params, m_args);
- loader = LoadImageCallback(&m_heightmapScale, loadHeightmap);
- }
- m_pDiffuse = evaluateTexture(m_template.m_diffuse, m_template.m_params, m_args);
- m_pBump = evaluateTexture(m_template.m_bump, m_template.m_params, m_args, loader);
- m_pSpecular = evaluateTexture(m_template.m_specular, m_template.m_params, m_args);
- m_pLightFalloffImage = evaluateTexture(m_template.m_lightFalloffImage, m_template.m_params, m_args);
-
- for(ShaderTemplate::MapLayers::const_iterator i = m_template.m_layers.begin(); i != m_template.m_layers.end(); ++i)
- {
- m_layers.push_back(evaluateLayer(*i, m_template.m_params, m_args));
- }
- }
- }
-
- void unrealiseLighting()
- {
- if(m_lightingEnabled)
- {
- GlobalTexturesCache().release(m_pDiffuse);
- GlobalTexturesCache().release(m_pBump);
- GlobalTexturesCache().release(m_pSpecular);
-
- GlobalTexturesCache().release(m_pLightFalloffImage);
-
- for(MapLayers::iterator i = m_layers.begin(); i != m_layers.end(); ++i)
- {
- GlobalTexturesCache().release((*i).texture());
- }
- }
- }
-
- // set shader name
- void setName(const char* name)
- {
- m_Name = name;
- }
-
- class MapLayer : public ShaderLayer
- {
- qtexture_t* m_texture;
- BlendFunc m_blendFunc;
- bool m_clampToBorder;
- float m_alphaTest;
- public:
- MapLayer(qtexture_t* texture, BlendFunc blendFunc, bool clampToBorder, float alphaTest) :
- m_texture(texture),
- m_blendFunc(blendFunc),
- m_clampToBorder(false),
- m_alphaTest(alphaTest)
- {
- }
- qtexture_t* texture() const
- {
- return m_texture;
- }
- BlendFunc blendFunc() const
- {
- return m_blendFunc;
- }
- bool clampToBorder() const
- {
- return m_clampToBorder;
- }
- float alphaTest() const
- {
- return m_alphaTest;
- }
- };
-
- static MapLayer evaluateLayer(const ShaderTemplate::MapLayerTemplate& layerTemplate, const ShaderParameters& params, const ShaderArguments& args)
- {
- return MapLayer(
- evaluateTexture(layerTemplate.texture(), params, args),
- evaluateBlendFunc(layerTemplate.blendFunc(), params, args),
- layerTemplate.clampToBorder(),
- evaluateFloat(layerTemplate.alphaTest(), params, args)
- );
- }
-
- typedef std::vector<MapLayer> MapLayers;
- MapLayers m_layers;
-
- const ShaderLayer* firstLayer() const
- {
- if(m_layers.empty())
- {
- return 0;
- }
- return &m_layers.front();
- }
- void forEachLayer(const ShaderLayerCallback& callback) const
- {
- for(MapLayers::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i)
- {
- callback(*i);
- }
- }
-
- qtexture_t* lightFalloffImage() const
- {
- if(!string_empty(m_template.m_lightFalloffImage.c_str()))
- {
- return m_pLightFalloffImage;
- }
- return 0;
- }
-};
-
-bool CShader::m_lightingEnabled = false;
-
-typedef SmartPointer<CShader> ShaderPointer;
-typedef std::map<CopiedString, ShaderPointer, shader_less_t> shaders_t;
-
-shaders_t g_ActiveShaders;
-
-static shaders_t::iterator g_ActiveShadersIterator;
-
-void ActiveShaders_IteratorBegin()
-{
- g_ActiveShadersIterator = g_ActiveShaders.begin();
-}
-
-bool ActiveShaders_IteratorAtEnd()
-{
- return g_ActiveShadersIterator == g_ActiveShaders.end();
-}
-
-IShader *ActiveShaders_IteratorCurrent()
-{
- return static_cast<CShader*>(g_ActiveShadersIterator->second);
-}
-
-void ActiveShaders_IteratorIncrement()
-{
- ++g_ActiveShadersIterator;
-}
-
-void debug_check_shaders(shaders_t& shaders)
-{
- for(shaders_t::iterator i = shaders.begin(); i != shaders.end(); ++i)
- {
- ASSERT_MESSAGE(i->second->refcount() == 1, "orphan shader still referenced");
- }
-}
-
-// will free all GL binded qtextures and shaders
-// NOTE: doesn't make much sense out of Radiant exit or called during a reload
-void FreeShaders()
-{
- // reload shaders
- // empty the actives shaders list
- debug_check_shaders(g_ActiveShaders);
- g_ActiveShaders.clear();
- g_shaders.clear();
- g_shaderTemplates.clear();
- g_shaderDefinitions.clear();
- g_ActiveShadersChangedNotify();
-}
-
-bool ShaderTemplate::parseQuake3(Tokeniser& tokeniser)
-{
- // name of the qtexture_t we'll use to represent this shader (this one has the "textures\" before)
- m_textureName = m_Name;
-
- tokeniser.nextLine();
-
- // we need to read until we hit a balanced }
- int depth = 0;
- for(;;)
- {
- tokeniser.nextLine();
- const char* token = tokeniser.getToken();
-
- if(token == 0)
- return false;
-
- if(string_equal(token, "{"))
- {
- ++depth;
- continue;
- }
- else if(string_equal(token, "}"))
- {
- --depth;
- if(depth < 0) // underflow
- {
- return false;
- }
- if(depth == 0) // end of shader
- {
- break;
- }
-
- continue;
- }
-
- if(depth == 1)
- {
- if (string_equal_nocase(token, "qer_nocarve"))
- {
- m_nFlags |= QER_NOCARVE;
- }
- else if (string_equal_nocase(token, "qer_trans"))
- {
- RETURN_FALSE_IF_FAIL(Tokeniser_getFloat(tokeniser, m_fTrans));
- m_nFlags |= QER_TRANS;
- }
- else if (string_equal_nocase(token, "qer_editorimage"))
- {
- RETURN_FALSE_IF_FAIL(Tokeniser_parseTextureName(tokeniser, m_textureName));
- }
- else if (string_equal_nocase(token, "qer_alphafunc"))
- {
- const char* alphafunc = tokeniser.getToken();
-
- if(alphafunc == 0)
- {
- Tokeniser_unexpectedError(tokeniser, alphafunc, "#alphafunc");
- return false;
- }
-
- if(string_equal_nocase(alphafunc, "equal"))
- {
- m_AlphaFunc = IShader::eEqual;
- }
- else if(string_equal_nocase(alphafunc, "greater"))
- {
- m_AlphaFunc = IShader::eGreater;
- }
- else if(string_equal_nocase(alphafunc, "less"))
- {
- m_AlphaFunc = IShader::eLess;
- }
- else if(string_equal_nocase(alphafunc, "gequal"))
- {
- m_AlphaFunc = IShader::eGEqual;
- }
- else if(string_equal_nocase(alphafunc, "lequal"))
- {
- m_AlphaFunc = IShader::eLEqual;
- }
- else
- {
- m_AlphaFunc = IShader::eAlways;
- }
-
- m_nFlags |= QER_ALPHATEST;
-
- RETURN_FALSE_IF_FAIL(Tokeniser_getFloat(tokeniser, m_AlphaRef));
- }
- else if (string_equal_nocase(token, "cull"))
- {
- const char* cull = tokeniser.getToken();
-
- if(cull == 0)
- {
- Tokeniser_unexpectedError(tokeniser, cull, "#cull");
- return false;
- }
-
- if(string_equal_nocase(cull, "none")
- || string_equal_nocase(cull, "twosided")
- || string_equal_nocase(cull, "disable"))
- {
- m_Cull = IShader::eCullNone;
- }
- else if(string_equal_nocase(cull, "back")
- || string_equal_nocase(cull, "backside")
- || string_equal_nocase(cull, "backsided"))
- {
- m_Cull = IShader::eCullBack;
- }
- else
- {
- m_Cull = IShader::eCullBack;
- }
-
- m_nFlags |= QER_CULL;
- }
- else if (string_equal_nocase(token, "surfaceparm"))
- {
- const char* surfaceparm = tokeniser.getToken();
-
- if(surfaceparm == 0)
- {
- Tokeniser_unexpectedError(tokeniser, surfaceparm, "#surfaceparm");
- return false;
- }
-
- if (string_equal_nocase(surfaceparm, "fog"))
- {
- m_nFlags |= QER_FOG;
- if (m_fTrans == 1.0f) // has not been explicitly set by qer_trans
- {
- m_fTrans = 0.35f;
- }
- }
- else if (string_equal_nocase(surfaceparm, "nodraw"))
- {
- m_nFlags |= QER_NODRAW;
- }
- else if (string_equal_nocase(surfaceparm, "nonsolid"))
- {
- m_nFlags |= QER_NONSOLID;
- }
- else if (string_equal_nocase(surfaceparm, "water"))
- {
- m_nFlags |= QER_WATER;
- }
- else if (string_equal_nocase(surfaceparm, "lava"))
- {
- m_nFlags |= QER_LAVA;
- }
- else if (string_equal_nocase(surfaceparm, "areaportal"))
- {
- m_nFlags |= QER_AREAPORTAL;
- }
- else if (string_equal_nocase(surfaceparm, "playerclip"))
- {
- m_nFlags |= QER_CLIP;
- }
- else if (string_equal_nocase(surfaceparm, "botclip"))
- {
- m_nFlags |= QER_BOTCLIP;
- }
- }
- }
- }
-
- return true;
-}
-
-class Layer
-{
-public:
- LayerTypeId m_type;
- CopiedString m_texture;
- BlendFunc m_blendFunc;
- bool m_clampToBorder;
- float m_alphaTest;
- float m_heightmapScale;
-
- Layer() : m_type(LAYER_NONE), m_blendFunc(BLEND_ONE, BLEND_ZERO), m_clampToBorder(false), m_alphaTest(-1), m_heightmapScale(0)
- {
- }
-};
-
-std::list<CopiedString> g_shaderFilenames;
-
-void ParseShaderFile(Tokeniser& tokeniser, const char* filename)
-{
- g_shaderFilenames.push_back(filename);
- filename = g_shaderFilenames.back().c_str();
- tokeniser.nextLine();
- for(;;)
- {
- const char* token = tokeniser.getToken();
-
- if(token == 0)
- {
- break;
- }
-
- if(string_equal(token, "table"))
- {
- if(tokeniser.getToken() == 0)
- {
- Tokeniser_unexpectedError(tokeniser, 0, "#table-name");
- return;
- }
- if(!Tokeniser_parseToken(tokeniser, "{"))
- {
- return;
- }
- for(;;)
- {
- const char* option = tokeniser.getToken();
- if(string_equal(option, "{"))
- {
- for(;;)
- {
- const char* value = tokeniser.getToken();
- if(string_equal(value, "}"))
- {
- break;
- }
- }
-
- if(!Tokeniser_parseToken(tokeniser, "}"))
- {
- return;
- }
- break;
- }
- }
- }
- else
- {
- if(string_equal(token, "guide"))
- {
- parseTemplateInstance(tokeniser, filename);
- }
- else
- {
- if(!string_equal(token, "material")
- && !string_equal(token, "particle")
- && !string_equal(token, "skin"))
- {
- tokeniser.ungetToken();
- }
- // first token should be the path + name.. (from base)
- CopiedString name;
- if(!Tokeniser_parseShaderName(tokeniser, name))
- {
- }
- ShaderTemplatePointer shaderTemplate(new ShaderTemplate());
- shaderTemplate->setName(name.c_str());
-
- g_shaders.insert(ShaderTemplateMap::value_type(shaderTemplate->getName(), shaderTemplate));
-
- bool result = (g_shaderLanguage == SHADERLANGUAGE_QUAKE3)
- ? shaderTemplate->parseQuake3(tokeniser)
- : shaderTemplate->parseDoom3(tokeniser);
- if (result)
- {
- // do we already have this shader?
- if(!g_shaderDefinitions.insert(ShaderDefinitionMap::value_type(shaderTemplate->getName(), ShaderDefinition(shaderTemplate.get(), ShaderArguments(), filename))).second)
- {
- #ifdef _DEBUG
- globalOutputStream() << "WARNING: shader " << shaderTemplate->getName() << " is already in memory, definition in " << filename << " ignored.\n";
- #endif
- }
- }
- else
- {
- globalErrorStream() << "Error parsing shader " << shaderTemplate->getName() << "\n";
- return;
- }
- }
- }
- }
-}
-
-void parseGuideFile(Tokeniser& tokeniser, const char* filename)
-{
- tokeniser.nextLine();
- for(;;)
- {
- const char* token = tokeniser.getToken();
-
- if(token == 0)
- {
- break;
- }
-
- if(string_equal(token, "guide"))
- {
- // first token should be the path + name.. (from base)
- ShaderTemplatePointer shaderTemplate(new ShaderTemplate);
- shaderTemplate->parseTemplate(tokeniser);
- if(!g_shaderTemplates.insert(ShaderTemplateMap::value_type(shaderTemplate->getName(), shaderTemplate)).second)
- {
- globalErrorStream() << "guide " << makeQuoted(shaderTemplate->getName()) << ": already defined, second definition ignored\n";
- }
- }
- else if(string_equal(token, "inlineGuide"))
- {
- // skip entire inlineGuide definition
- std::size_t depth = 0;
- for(;;)
- {
- tokeniser.nextLine();
- token = tokeniser.getToken();
- if(string_equal(token, "{"))
- {
- ++depth;
- }
- else if(string_equal(token, "}"))
- {
- if(--depth == 0)
- {
- break;
- }
- }
- }
- }
- }
-}
-
-void LoadShaderFile(const char* filename)
-{
- ArchiveTextFile* file = GlobalFileSystem().openTextFile(filename);
-
- if(file != 0)
- {
- globalOutputStream() << "Parsing shaderfile " << filename << "\n";
-
- Tokeniser& tokeniser = GlobalScriptLibrary().m_pfnNewScriptTokeniser(file->getInputStream());
-
- ParseShaderFile(tokeniser, filename);
-
- tokeniser.release();
- file->release();
- }
- else
- {
- globalOutputStream() << "Unable to read shaderfile " << filename << "\n";
- }
-}
-
-typedef FreeCaller1<const char*, LoadShaderFile> LoadShaderFileCaller;
-
-
-void loadGuideFile(const char* filename)
-{
- StringOutputStream fullname(256);
- fullname << "guides/" << filename;
- ArchiveTextFile* file = GlobalFileSystem().openTextFile(fullname.c_str());
-
- if(file != 0)
- {
- globalOutputStream() << "Parsing guide file " << fullname.c_str() << "\n";
-
- Tokeniser& tokeniser = GlobalScriptLibrary().m_pfnNewScriptTokeniser(file->getInputStream());
-
- parseGuideFile(tokeniser, fullname.c_str());