OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
StdMeshMatManager Class Reference

#include <StdMeshMaterial.h>

Classes

class  Iterator
 

Public Types

enum  ShaderLoadFlag { SMM_AcceptExisting = 1, SMM_ForceReload = 2 }
 

Public Member Functions

void Clear ()
 
std::set< StdCopyStrBufParse (const char *mat_script, const char *filename, StdMeshMaterialLoader &loader)
 
const StdMeshMaterialGetMaterial (const char *material_name) const
 
Iterator Begin ()
 
Iterator End ()
 
void Remove (const StdStrBuf &name, class StdMeshMaterialUpdate *update)
 
Iterator Remove (const Iterator &iter, class StdMeshMaterialUpdate *update)
 
const StdMeshMaterialShaderAddShader (const char *filename, const char *name, const char *language, StdMeshMaterialShaderType type, const char *text, uint32_t load_flags)
 
const StdMeshMaterialProgramAddProgram (const char *name, StdMeshMaterialLoader &loader, const StdMeshMaterialPass::ShaderInstance &fragment_shader, const StdMeshMaterialPass::ShaderInstance &vertex_shader, const StdMeshMaterialPass::ShaderInstance &geometry_shader)
 
const StdMeshMaterialShaderGetFragmentShader (const char *name) const
 
const StdMeshMaterialShaderGetVertexShader (const char *name) const
 
const StdMeshMaterialShaderGetGeometryShader (const char *name) const
 

Friends

class StdMeshMaterialUpdate
 

Detailed Description

Definition at line 520 of file StdMeshMaterial.h.

Member Enumeration Documentation

Enumerator
SMM_AcceptExisting 
SMM_ForceReload 

Definition at line 527 of file StdMeshMaterial.h.

Member Function Documentation

const StdMeshMaterialProgram * StdMeshMatManager::AddProgram ( const char *  name,
StdMeshMaterialLoader loader,
const StdMeshMaterialPass::ShaderInstance fragment_shader,
const StdMeshMaterialPass::ShaderInstance vertex_shader,
const StdMeshMaterialPass::ShaderInstance geometry_shader 
)

Definition at line 1690 of file StdMeshMaterial.cpp.

References StdMeshMaterialProgram::AddParameterNames(), StdMeshMaterialProgram::Compile(), StdMeshMaterialProgram::IsCompiled(), StdMeshMaterialPass::ShaderInstance::Parameters, and StdMeshMaterialPass::ShaderInstance::Shader.

Referenced by CStdGL::PrepareMaterial().

1691 {
1692  std::tuple<const StdMeshMaterialShader*, const StdMeshMaterialShader*, const StdMeshMaterialShader*> key = std::make_tuple(fragment_shader.Shader, vertex_shader.Shader, geometry_shader.Shader);
1693  ProgramMap::iterator iter = Programs.find(key);
1694  if(iter == Programs.end())
1695  {
1696  std::unique_ptr<StdMeshMaterialProgram> program(new StdMeshMaterialProgram(name, fragment_shader.Shader, vertex_shader.Shader, geometry_shader.Shader));
1697  iter = Programs.insert(std::make_pair(key, std::move(program))).first;
1698  }
1699 
1700  StdMeshMaterialProgram& inserted_program = *iter->second;
1701 
1702  const bool fragment_added = inserted_program.AddParameterNames(fragment_shader.Parameters);
1703  const bool vertex_added = inserted_program.AddParameterNames(vertex_shader.Parameters);
1704  const bool geometry_added = inserted_program.AddParameterNames(geometry_shader.Parameters);
1705 
1706  // Re-compile the program (and assign new uniform locations if new
1707  // parameters were encountered).
1708  if(!inserted_program.IsCompiled() || fragment_added || vertex_added || geometry_added)
1709  if(!inserted_program.Compile(loader))
1710  return nullptr;
1711 
1712  return &inserted_program;
1713 }
StdMeshMaterialShaderParameters Parameters
const StdMeshMaterialShader * Shader
bool Compile(StdMeshMaterialLoader &loader)
bool AddParameterNames(const StdMeshMaterialShaderParameters &parameters)

Here is the call graph for this function:

Here is the caller graph for this function:

const StdMeshMaterialShader * StdMeshMatManager::AddShader ( const char *  filename,
const char *  name,
const char *  language,
StdMeshMaterialShaderType  type,
const char *  text,
uint32_t  load_flags 
)

Definition at line 1652 of file StdMeshMaterial.cpp.

References SMM_AcceptExisting, SMM_ForceReload, SMMS_FRAGMENT, SMMS_GEOMETRY, and SMMS_VERTEX.

Referenced by LoadShader(), and CStdGL::PrepareMaterial().

1653 {
1654  ShaderMap* map = nullptr;
1655  switch(type)
1656  {
1657  case SMMS_FRAGMENT:
1658  map = &FragmentShaders;
1659  break;
1660  case SMMS_VERTEX:
1661  map = &VertexShaders;
1662  break;
1663  case SMMS_GEOMETRY:
1664  map = &GeometryShaders;
1665  break;
1666  }
1667 
1668  StdCopyStrBuf name_buf(name);
1669  ShaderMap::iterator iter = map->find(name_buf);
1670 
1671  if(iter != map->end())
1672  {
1673  // Shader exists
1674  if ((load_flags & SMM_ForceReload) == SMM_ForceReload)
1675  map->erase(iter);
1676  else if ((load_flags & SMM_AcceptExisting) == SMM_AcceptExisting)
1677  return iter->second.get();
1678  else
1679  return nullptr;
1680  }
1681 
1682  std::unique_ptr<StdMeshMaterialShader> shader(new StdMeshMaterialShader(filename, name, language, type, text));
1683  std::pair<ShaderMap::iterator, bool> inserted = map->insert(std::make_pair(name_buf, std::move(shader)));
1684  assert(inserted.second == true);
1685  iter = inserted.first;
1686 
1687  return iter->second.get();
1688 }

Here is the caller graph for this function:

Iterator StdMeshMatManager::Begin ( )
inline

Definition at line 564 of file StdMeshMaterial.h.

Referenced by C4DefGraphicsPtrBackup::Add().

564 { return Iterator(Materials.begin()); }

Here is the caller graph for this function:

void StdMeshMatManager::Clear ( )

Definition at line 1509 of file StdMeshMaterial.cpp.

Referenced by C4Game::Clear().

1510 {
1511  Materials.clear();
1512 
1513  Programs.clear();
1514  FragmentShaders.clear();
1515  VertexShaders.clear();
1516  GeometryShaders.clear();
1517 }

Here is the caller graph for this function:

Iterator StdMeshMatManager::End ( )
inline

Definition at line 565 of file StdMeshMaterial.h.

Referenced by C4DefGraphicsPtrBackup::Add().

565 { return Iterator(Materials.end()); }

Here is the caller graph for this function:

const StdMeshMaterialShader * StdMeshMatManager::GetFragmentShader ( const char *  name) const

Definition at line 1631 of file StdMeshMaterial.cpp.

1632 {
1633  ShaderMap::const_iterator iter = FragmentShaders.find(StdCopyStrBuf(name));
1634  if(iter == FragmentShaders.end()) return nullptr;
1635  return iter->second.get();
1636 }
const StdMeshMaterialShader * StdMeshMatManager::GetGeometryShader ( const char *  name) const

Definition at line 1645 of file StdMeshMaterial.cpp.

1646 {
1647  ShaderMap::const_iterator iter = GeometryShaders.find(StdCopyStrBuf(name));
1648  if(iter == GeometryShaders.end()) return nullptr;
1649  return iter->second.get();
1650 }
const StdMeshMaterial * StdMeshMatManager::GetMaterial ( const char *  material_name) const

Definition at line 1606 of file StdMeshMaterial.cpp.

Referenced by StdSubMeshInstance::CompileFunc(), StdMeshLoader::LoadMeshBinary(), StdMeshLoader::LoadMeshXml(), and StdMeshMaterialUpdate::Update().

1607 {
1608  std::map<StdCopyStrBuf, StdMeshMaterial>::const_iterator iter = Materials.find(StdCopyStrBuf(material_name));
1609  if (iter == Materials.end()) return nullptr;
1610  return &iter->second;
1611 }

Here is the caller graph for this function:

const StdMeshMaterialShader * StdMeshMatManager::GetVertexShader ( const char *  name) const

Definition at line 1638 of file StdMeshMaterial.cpp.

1639 {
1640  ShaderMap::const_iterator iter = VertexShaders.find(StdCopyStrBuf(name));
1641  if(iter == VertexShaders.end()) return nullptr;
1642  return iter->second.get();
1643 }
std::set< StdCopyStrBuf > StdMeshMatManager::Parse ( const char *  mat_script,
const char *  filename,
StdMeshMaterialLoader loader 
)

Definition at line 1519 of file StdMeshMaterial.cpp.

References StdMeshMaterialParserCtx::Advance(), StdMeshMaterialParserCtx::AdvanceRequired(), StdMeshMaterialParserCtx::Error(), StdMeshMaterialParserCtx::ErrorUnexpectedIdentifier(), StdMeshMaterialParserCtx::FileName, StdMeshMaterial::FileName, FormatString(), StdStrBuf::getData(), StdMeshMaterialParserCtx::Line, StdMeshMaterial::Line, StdMeshMaterial::Load(), LoadShader(), StdMeshMaterial::Name, pDraw, C4Draw::PrepareMaterial(), SMMS_FRAGMENT, SMMS_GEOMETRY, SMMS_VERTEX, TOKEN_BRACE_OPEN, TOKEN_COLON, TOKEN_EOF, and TOKEN_IDTF.

1520 {
1521  StdMeshMaterialParserCtx ctx(*this, mat_script, filename, loader);
1522 
1523  Token token;
1524  StdCopyStrBuf token_name;
1525 
1526  std::set<StdCopyStrBuf> loaded_materials;
1527 
1528  while ((token = ctx.Advance(token_name)) == TOKEN_IDTF)
1529  {
1530  if (token_name == "material")
1531  {
1532  // Read name
1533  StdCopyStrBuf material_name;
1534  ctx.AdvanceRequired(material_name, TOKEN_IDTF);
1535 
1536  // Check for uniqueness
1537  std::map<StdCopyStrBuf, StdMeshMaterial>::iterator iter = Materials.find(material_name);
1538  if (iter != Materials.end())
1539  ctx.Error(FormatString("Material with name '%s' is already defined in %s:%u", material_name.getData(), iter->second.FileName.getData(), iter->second.Line));
1540 
1541  // Check if there is a parent given
1542  Token next = ctx.AdvanceRequired(token_name, TOKEN_BRACE_OPEN, TOKEN_COLON);
1543  // Read parent name, if any
1544  StdMeshMaterial* parent = nullptr;
1545  if (next == TOKEN_COLON)
1546  {
1547  // Note that if there is a parent, then it needs to be loaded
1548  // already. This currently makes only sense when its defined above
1549  // in the same material script file or in a parent definition.
1550  // We could later support material scripts in the System.ocg.
1551  StdCopyStrBuf parent_name;
1552  ctx.AdvanceRequired(parent_name, TOKEN_IDTF);
1553  ctx.AdvanceRequired(token_name, TOKEN_BRACE_OPEN);
1554 
1555  iter = Materials.find(parent_name);
1556  if (iter == Materials.end())
1557  ctx.Error(StdCopyStrBuf("Parent material '") + parent_name + "' does not exist (or is not yet loaded)");
1558  parent = &iter->second;
1559  }
1560 
1561  // Copy properties from parent if one is given, otherwise
1562  // default-construct the material.
1563  StdMeshMaterial mat = parent ? StdMeshMaterial(*parent) : StdMeshMaterial();
1564 
1565  // Set/Overwrite source and name
1566  mat.Name = material_name;
1567  mat.FileName = ctx.FileName;
1568  mat.Line = ctx.Line;
1569 
1570  mat.Load(ctx);
1571 
1572  Materials[material_name] = mat;
1573 
1574 #ifndef USE_CONSOLE
1575  // To Gfxspecific setup of the material; choose working techniques
1576  if (!pDraw->PrepareMaterial(*this, loader, Materials[material_name]))
1577  {
1578  Materials.erase(material_name);
1579  ctx.Error(StdCopyStrBuf("No working technique for material '") + material_name + "'");
1580  }
1581 #endif
1582  loaded_materials.insert(material_name);
1583  }
1584  else if (token_name == "vertex_program")
1585  {
1586  LoadShader(ctx, SMMS_VERTEX);
1587  }
1588  else if (token_name == "fragment_program")
1589  {
1590  LoadShader(ctx, SMMS_FRAGMENT);
1591  }
1592  else if (token_name == "geometry_program")
1593  {
1594  LoadShader(ctx, SMMS_GEOMETRY);
1595  }
1596  else
1597  ctx.ErrorUnexpectedIdentifier(token_name);
1598  }
1599 
1600  if (token != TOKEN_EOF)
1601  ctx.Error(StdCopyStrBuf("'") + token_name.getData() + "' unexpected");
1602 
1603  return loaded_materials;
1604 }
const char * getData() const
Definition: StdBuf.h:450
StdCopyStrBuf FileName
virtual bool PrepareMaterial(StdMeshMatManager &mat_manager, StdMeshMaterialLoader &loader, StdMeshMaterial &mat)=0
void LoadShader(StdMeshMaterialParserCtx &ctx, StdMeshMaterialShaderType type)
C4Draw * pDraw
Definition: C4Draw.cpp:45
unsigned int Line
void Load(StdMeshMaterialParserCtx &ctx)
StdCopyStrBuf Name
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:277

Here is the call graph for this function:

void StdMeshMatManager::Remove ( const StdStrBuf name,
class StdMeshMaterialUpdate update 
)

Definition at line 1622 of file StdMeshMaterial.cpp.

Referenced by C4DefGraphicsPtrBackup::Add().

1623 {
1624  auto it = Materials.find(StdCopyStrBuf(name));
1625  if (it == Materials.end())
1626  return;
1627  if (update) update->Add(&it->second);
1628  Materials.erase(it);
1629 }

Here is the caller graph for this function:

StdMeshMatManager::Iterator StdMeshMatManager::Remove ( const Iterator iter,
class StdMeshMaterialUpdate update 
)

Definition at line 1613 of file StdMeshMaterial.cpp.

1614 {
1615  if(update) update->Add(&*iter);
1616  Iterator next_iter = iter;
1617  ++next_iter;
1618  Materials.erase(iter.iter_);
1619  return next_iter;
1620 }

Friends And Related Function Documentation

friend class StdMeshMaterialUpdate
friend

Definition at line 522 of file StdMeshMaterial.h.


The documentation for this class was generated from the following files: