Index: ps/trunk/source/graphics/ShaderDefines.cpp =================================================================== --- ps/trunk/source/graphics/ShaderDefines.cpp (revision 11493) +++ ps/trunk/source/graphics/ShaderDefines.cpp (revision 11494) @@ -1,241 +1,243 @@ /* Copyright (C) 2012 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "ShaderDefines.h" #include "maths/Vector4D.h" #include "ps/ThreadUtil.h" +#include + size_t hash_value(const CStrIntern& v) { return v.GetHash(); } size_t hash_value(const CVector4D& v) { size_t hash = 0; boost::hash_combine(hash, v.X); boost::hash_combine(hash, v.Y); boost::hash_combine(hash, v.Z); boost::hash_combine(hash, v.W); return hash; } size_t hash_value(const CShaderParams::SItems& items) { return items.hash; } size_t hash_value(const CShaderParams::SItems& items) { return items.hash; } bool operator==(const CShaderParams::SItems& a, const CShaderParams::SItems& b) { return a.items == b.items; } bool operator==(const CShaderParams::SItems& a, const CShaderParams::SItems& b) { return a.items == b.items; } template struct ItemNameCmp { typedef typename CShaderParams::SItems::Item Item; typedef Item first_argument_type; typedef Item second_argument_type; bool operator()(const Item& a, const Item& b) const { return a.first < b.first; } }; template struct ItemNameGeq { typedef typename CShaderParams::SItems::Item Item; bool operator()(const Item& a, const Item& b) const { return !(b.first < a.first); } }; template typename CShaderParams::SItems* CShaderParams::GetInterned(const SItems& items) { ENSURE(ThreadUtil::IsMainThread()); // s_InternedItems is not thread-safe typename InternedItems_t::iterator it = s_InternedItems.find(items); if (it != s_InternedItems.end()) return it->second.get(); // Sanity test: the items list is meant to be sorted by name. // This is a reasonable place to verify that, since this will be called once per distinct SItems. typedef ItemNameCmp Cmp; ENSURE(std::adjacent_find(items.items.begin(), items.items.end(), std::binary_negate(Cmp())) == items.items.end()); shared_ptr ptr(new SItems(items)); s_InternedItems.insert(std::make_pair(items, ptr)); return ptr.get(); } template CShaderParams::CShaderParams() { SItems items; items.RecalcHash(); m_Items = GetInterned(items); } template void CShaderParams::Set(CStrIntern name, const value_t& value) { SItems items = *m_Items; typename SItems::Item addedItem = std::make_pair(name, value); // Add the new item in a way that preserves the sortedness and uniqueness of item names for (typename std::vector::iterator it = items.items.begin(); ; ++it) { if (it == items.items.end() || addedItem.first < it->first) { items.items.insert(it, addedItem); break; } else if (addedItem.first == it->first) { it->second = addedItem.second; break; } } items.RecalcHash(); m_Items = GetInterned(items); } template void CShaderParams::SetMany(const CShaderParams& params) { SItems items; // set_union merges the two sorted lists into a new sorted list; // if two items are equivalent (i.e. equal names, possibly different values) // then the one from the first list is kept std::set_union( params.m_Items->items.begin(), params.m_Items->items.end(), m_Items->items.begin(), m_Items->items.end(), std::inserter(items.items, items.items.begin()), ItemNameCmp()); items.RecalcHash(); m_Items = GetInterned(items); } template std::map CShaderParams::GetMap() const { std::map ret; for (size_t i = 0; i < m_Items->items.size(); ++i) ret[m_Items->items[i].first] = m_Items->items[i].second; return ret; } template size_t CShaderParams::GetHash() const { return m_Items->hash; } template void CShaderParams::SItems::RecalcHash() { size_t h = 0; for (size_t i = 0; i < items.size(); ++i) { boost::hash_combine(h, items[i].first); boost::hash_combine(h, items[i].second); } hash = h; } void CShaderDefines::Add(const char* name, const char* value) { Set(CStrIntern(name), CStrIntern(value)); } int CShaderDefines::GetInt(const char* name) const { CStrIntern nameIntern(name); for (size_t i = 0; i < m_Items->items.size(); ++i) { if (m_Items->items[i].first == nameIntern) { int ret; std::stringstream str(m_Items->items[i].second.c_str()); str >> ret; return ret; } } return 0; } void CShaderUniforms::Add(const char* name, const CVector4D& value) { Set(CStrIntern(name), value); } CVector4D CShaderUniforms::GetVector(const char* name) const { CStrIntern nameIntern(name); for (size_t i = 0; i < m_Items->items.size(); ++i) { if (m_Items->items[i].first == nameIntern) { return m_Items->items[i].second; } } return CVector4D(); } void CShaderUniforms::BindUniforms(const CShaderProgramPtr& shader) const { const std::vector& items = m_Items->items; for (size_t i = 0; i < items.size(); ++i) { CShaderProgram::Binding binding = shader->GetUniformBinding(items[i].first); if (binding.Active()) { CVector4D v = items[i].second; shader->Uniform(binding, v.X, v.Y, v.Z, v.W); } } } // Explicit instantiations: template<> CShaderParams::InternedItems_t CShaderParams::s_InternedItems = CShaderParams::InternedItems_t(); template<> CShaderParams::InternedItems_t CShaderParams::s_InternedItems = CShaderParams::InternedItems_t(); template class CShaderParams; template class CShaderParams; Index: ps/trunk/source/graphics/MaterialManager.cpp =================================================================== --- ps/trunk/source/graphics/MaterialManager.cpp (revision 11493) +++ ps/trunk/source/graphics/MaterialManager.cpp (revision 11494) @@ -1,100 +1,101 @@ /* Copyright (C) 2012 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "MaterialManager.h" #include "lib/ogl.h" #include "maths/Vector4D.h" #include "ps/Filesystem.h" #include "ps/PreprocessorWrapper.h" #include "ps/XML/Xeromyces.h" #include "renderer/Renderer.h" +#include + CMaterial CMaterialManager::LoadMaterial(const VfsPath& pathname) { if (pathname.empty()) return CMaterial(); std::map::iterator iter = m_Materials.find(pathname); if (iter != m_Materials.end()) return iter->second; CXeromyces xeroFile; if (xeroFile.Load(g_VFS, pathname) != PSRETURN_OK) return CMaterial(); #define EL(x) int el_##x = xeroFile.GetElementID(#x) #define AT(x) int at_##x = xeroFile.GetAttributeID(#x) EL(alpha_blending); EL(alternative); EL(define); EL(shader); EL(uniform); AT(effect); AT(if); AT(material); AT(name); AT(value); #undef AT #undef EL CMaterial material; XMBElement root = xeroFile.GetRoot(); - XMBElementList childNodes = root.GetChildNodes(); CPreprocessorWrapper preprocessor; preprocessor.AddDefine("CFG_FORCE_ALPHATEST", g_Renderer.m_Options.m_ForceAlphaTest ? "1" : "0"); XERO_ITER_EL(root, node) { int token = node.GetNodeName(); XMBAttributeList attrs = node.GetAttributes(); if (token == el_alternative) { if (preprocessor.TestConditional(attrs.GetNamedItem(at_if))) { material = LoadMaterial(VfsPath("art/materials") / attrs.GetNamedItem(at_material).FromUTF8()); break; } } else if (token == el_alpha_blending) { material.SetUsesAlphaBlending(true); } else if (token == el_shader) { material.SetShaderEffect(attrs.GetNamedItem(at_effect)); } else if (token == el_define) { material.AddShaderDefine(attrs.GetNamedItem(at_name).c_str(), attrs.GetNamedItem(at_value).c_str()); } else if (token == el_uniform) { std::stringstream str(attrs.GetNamedItem(at_value)); CVector4D vec; str >> vec.X >> vec.Y >> vec.Z >> vec.W; material.AddStaticUniform(attrs.GetNamedItem(at_name).c_str(), vec); } } m_Materials[pathname] = material; return material; }