/* Copyright (C) 2015 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 "graphics/ShaderProgram.h"
#include "maths/Vector4D.h"
#include "ps/ThreadUtil.h"
#include
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()
{
*this = s_Empty;
}
template
CShaderParams::CShaderParams(SItems* items) : m_Items(items)
{
}
template
CShaderParams CShaderParams::CreateEmpty()
{
SItems items;
items.RecalcHash();
return CShaderParams(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(CStrIntern name, CStrIntern value)
{
Set(name, 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);
}
}
}
void CShaderRenderQueries::Add(const char* name)
{
if (name == CStr("sim_time"))
{
m_Items.emplace_back(RQUERY_TIME, CStrIntern(name));
}
else if (name == CStr("water_tex"))
{
m_Items.emplace_back(RQUERY_WATER_TEX, CStrIntern(name));
}
else if (name == CStr("sky_cube"))
{
m_Items.emplace_back(RQUERY_SKY_CUBE, CStrIntern(name));
}
}
void CShaderConditionalDefines::Add(const char* defname, const char* defvalue, int type, std::vector &args)
{
CondDefine cd;
cd.m_DefName = CStrIntern(defname);
cd.m_DefValue = CStrIntern(defvalue);
cd.m_CondArgs = args;
cd.m_CondType = type;
m_Defines.push_back(cd);
}
// Explicit instantiations:
template<> CShaderParams::InternedItems_t CShaderParams::s_InternedItems = CShaderParams::InternedItems_t();
template<> CShaderParams::InternedItems_t CShaderParams::s_InternedItems = CShaderParams::InternedItems_t();
template<> CShaderParams CShaderParams::s_Empty = CShaderParams::CreateEmpty();
template<> CShaderParams CShaderParams::s_Empty = CShaderParams::CreateEmpty();
template class CShaderParams;
template class CShaderParams;