OpenClonk
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 517 of file StdMeshMaterial.h.

Member Enumeration Documentation

◆ ShaderLoadFlag

Enumerator
SMM_AcceptExisting 
SMM_ForceReload 

Definition at line 524 of file StdMeshMaterial.h.

Member Function Documentation

◆ AddProgram()

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 1664 of file StdMeshMaterial.cpp.

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

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

1665 {
1666  std::tuple<const StdMeshMaterialShader*, const StdMeshMaterialShader*, const StdMeshMaterialShader*> key = std::make_tuple(fragment_shader.Shader, vertex_shader.Shader, geometry_shader.Shader);
1667  ProgramMap::iterator iter = Programs.find(key);
1668  if(iter == Programs.end())
1669  {
1670  std::unique_ptr<StdMeshMaterialProgram> program(new StdMeshMaterialProgram(name, fragment_shader.Shader, vertex_shader.Shader, geometry_shader.Shader));
1671  iter = Programs.insert(std::make_pair(key, std::move(program))).first;
1672  }
1673 
1674  StdMeshMaterialProgram& inserted_program = *iter->second;
1675 
1676  const bool fragment_added = inserted_program.AddParameterNames(fragment_shader.Parameters);
1677  const bool vertex_added = inserted_program.AddParameterNames(vertex_shader.Parameters);
1678  const bool geometry_added = inserted_program.AddParameterNames(geometry_shader.Parameters);
1679 
1680  // Re-compile the program (and assign new uniform locations if new
1681  // parameters were encountered).
1682  if(!inserted_program.IsCompiled() || fragment_added || vertex_added || geometry_added)
1683  if(!inserted_program.Compile(loader))
1684  return nullptr;
1685 
1686  return &inserted_program;
1687 }
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:

◆ AddShader()

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 1626 of file StdMeshMaterial.cpp.

References SMMS_FRAGMENT, SMMS_GEOMETRY, and SMMS_VERTEX.

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

1627 {
1628  ShaderMap* map = nullptr;
1629  switch(type)
1630  {
1631  case SMMS_FRAGMENT:
1632  map = &FragmentShaders;
1633  break;
1634  case SMMS_VERTEX:
1635  map = &VertexShaders;
1636  break;
1637  case SMMS_GEOMETRY:
1638  map = &GeometryShaders;
1639  break;
1640  }
1641 
1642  StdCopyStrBuf name_buf(name);
1643  ShaderMap::iterator iter = map->find(name_buf);
1644 
1645  if(iter != map->end())
1646  {
1647  // Shader exists
1648  if ((load_flags & SMM_ForceReload) == SMM_ForceReload)
1649  map->erase(iter);
1650  else if ((load_flags & SMM_AcceptExisting) == SMM_AcceptExisting)
1651  return iter->second.get();
1652  else
1653  return nullptr;
1654  }
1655 
1656  std::unique_ptr<StdMeshMaterialShader> shader(new StdMeshMaterialShader(filename, name, language, type, text));
1657  std::pair<ShaderMap::iterator, bool> inserted = map->insert(std::make_pair(name_buf, std::move(shader)));
1658  assert(inserted.second == true);
1659  iter = inserted.first;
1660 
1661  return iter->second.get();
1662 }
Here is the caller graph for this function:

◆ Begin()

Iterator StdMeshMatManager::Begin ( )
inline

Definition at line 561 of file StdMeshMaterial.h.

Referenced by C4DefGraphicsPtrBackup::Add().

561 { return Iterator(Materials.begin()); }
Here is the caller graph for this function:

◆ Clear()

void StdMeshMatManager::Clear ( )

Definition at line 1483 of file StdMeshMaterial.cpp.

Referenced by C4Game::Clear(), and StdMeshMatManager::Iterator::operator->().

1484 {
1485  Materials.clear();
1486 
1487  Programs.clear();
1488  FragmentShaders.clear();
1489  VertexShaders.clear();
1490  GeometryShaders.clear();
1491 }
Here is the caller graph for this function:

◆ End()

Iterator StdMeshMatManager::End ( )
inline

Definition at line 562 of file StdMeshMaterial.h.

References AddProgram(), AddShader(), GetFragmentShader(), GetGeometryShader(), GetVertexShader(), MeshMaterialManager, Remove(), and C4ScriptGuiWindowPropertyName::text.

Referenced by C4DefGraphicsPtrBackup::Add().

562 { return Iterator(Materials.end()); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFragmentShader()

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

Definition at line 1605 of file StdMeshMaterial.cpp.

Referenced by End(), and StdMeshMaterialPass::StdMeshMaterialPass().

1606 {
1607  ShaderMap::const_iterator iter = FragmentShaders.find(StdCopyStrBuf(name));
1608  if(iter == FragmentShaders.end()) return nullptr;
1609  return iter->second.get();
1610 }
Here is the caller graph for this function:

◆ GetGeometryShader()

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

Definition at line 1619 of file StdMeshMaterial.cpp.

Referenced by End(), and StdMeshMaterialPass::StdMeshMaterialPass().

1620 {
1621  ShaderMap::const_iterator iter = GeometryShaders.find(StdCopyStrBuf(name));
1622  if(iter == GeometryShaders.end()) return nullptr;
1623  return iter->second.get();
1624 }
Here is the caller graph for this function:

◆ GetMaterial()

const StdMeshMaterial * StdMeshMatManager::GetMaterial ( const char *  material_name) const

Definition at line 1580 of file StdMeshMaterial.cpp.

Referenced by StdSubMeshInstance::CompileFunc(), StdMeshLoader::LoadMeshBinary(), StdMeshLoader::LoadMeshXml(), StdMeshMatManager::Iterator::operator->(), and StdMeshMaterialUpdate::Update().

1581 {
1582  std::map<StdCopyStrBuf, StdMeshMaterial>::const_iterator iter = Materials.find(StdCopyStrBuf(material_name));
1583  if (iter == Materials.end()) return nullptr;
1584  return &iter->second;
1585 }
Here is the caller graph for this function:

◆ GetVertexShader()

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

Definition at line 1612 of file StdMeshMaterial.cpp.

Referenced by End(), and StdMeshMaterialPass::StdMeshMaterialPass().

1613 {
1614  ShaderMap::const_iterator iter = VertexShaders.find(StdCopyStrBuf(name));
1615  if(iter == VertexShaders.end()) return nullptr;
1616  return iter->second.get();
1617 }
Here is the caller graph for this function:

◆ Parse()

std::set< StdCopyStrBuf > StdMeshMatManager::Parse ( const char *  mat_script,
const char *  filename,
StdMeshMaterialLoader loader 
)

Definition at line 1493 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.

Referenced by C4Def::Clear(), and StdMeshMatManager::Iterator::operator->().

1494 {
1495  StdMeshMaterialParserCtx ctx(*this, mat_script, filename, loader);
1496 
1497  Token token;
1498  StdCopyStrBuf token_name;
1499 
1500  std::set<StdCopyStrBuf> loaded_materials;
1501 
1502  while ((token = ctx.Advance(token_name)) == TOKEN_IDTF)
1503  {
1504  if (token_name == "material")
1505  {
1506  // Read name
1507  StdCopyStrBuf material_name;
1508  ctx.AdvanceRequired(material_name, TOKEN_IDTF);
1509 
1510  // Check for uniqueness
1511  std::map<StdCopyStrBuf, StdMeshMaterial>::iterator iter = Materials.find(material_name);
1512  if (iter != Materials.end())
1513  ctx.Error(FormatString("Material with name '%s' is already defined in %s:%u", material_name.getData(), iter->second.FileName.getData(), iter->second.Line));
1514 
1515  // Check if there is a parent given
1516  Token next = ctx.AdvanceRequired(token_name, TOKEN_BRACE_OPEN, TOKEN_COLON);
1517  // Read parent name, if any
1518  StdMeshMaterial* parent = nullptr;
1519  if (next == TOKEN_COLON)
1520  {
1521  // Note that if there is a parent, then it needs to be loaded
1522  // already. This currently makes only sense when its defined above
1523  // in the same material script file or in a parent definition.
1524  // We could later support material scripts in the System.ocg.
1525  StdCopyStrBuf parent_name;
1526  ctx.AdvanceRequired(parent_name, TOKEN_IDTF);
1527  ctx.AdvanceRequired(token_name, TOKEN_BRACE_OPEN);
1528 
1529  iter = Materials.find(parent_name);
1530  if (iter == Materials.end())
1531  ctx.Error(StdCopyStrBuf("Parent material '") + parent_name + "' does not exist (or is not yet loaded)");
1532  parent = &iter->second;
1533  }
1534 
1535  // Copy properties from parent if one is given, otherwise
1536  // default-construct the material.
1537  StdMeshMaterial mat = parent ? StdMeshMaterial(*parent) : StdMeshMaterial();
1538 
1539  // Set/Overwrite source and name
1540  mat.Name = material_name;
1541  mat.FileName = ctx.FileName;
1542  mat.Line = ctx.Line;
1543 
1544  mat.Load(ctx);
1545 
1546  Materials[material_name] = mat;
1547 
1548 #ifndef USE_CONSOLE
1549  // To Gfxspecific setup of the material; choose working techniques
1550  if (!pDraw->PrepareMaterial(*this, loader, Materials[material_name]))
1551  {
1552  Materials.erase(material_name);
1553  ctx.Error(StdCopyStrBuf("No working technique for material '") + material_name + "'");
1554  }
1555 #endif
1556  loaded_materials.insert(material_name);
1557  }
1558  else if (token_name == "vertex_program")
1559  {
1560  LoadShader(ctx, SMMS_VERTEX);
1561  }
1562  else if (token_name == "fragment_program")
1563  {
1564  LoadShader(ctx, SMMS_FRAGMENT);
1565  }
1566  else if (token_name == "geometry_program")
1567  {
1568  LoadShader(ctx, SMMS_GEOMETRY);
1569  }
1570  else
1571  ctx.ErrorUnexpectedIdentifier(token_name);
1572  }
1573 
1574  if (token != TOKEN_EOF)
1575  ctx.Error(StdCopyStrBuf("'") + token_name.getData() + "' unexpected");
1576 
1577  return loaded_materials;
1578 }
StdCopyStrBuf FileName
virtual bool PrepareMaterial(StdMeshMatManager &mat_manager, StdMeshMaterialLoader &loader, StdMeshMaterial &mat)=0
void LoadShader(StdMeshMaterialParserCtx &ctx, StdMeshMaterialShaderType type)
C4Draw * pDraw
Definition: C4Draw.cpp:42
const char * getData() const
Definition: StdBuf.h:442
unsigned int Line
void Load(StdMeshMaterialParserCtx &ctx)
StdCopyStrBuf Name
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Remove() [1/2]

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

Definition at line 1596 of file StdMeshMaterial.cpp.

Referenced by C4DefGraphicsPtrBackup::Add(), C4Def::Clear(), and End().

1597 {
1598  auto it = Materials.find(StdCopyStrBuf(name));
1599  if (it == Materials.end())
1600  return;
1601  if (update) update->Add(&it->second);
1602  Materials.erase(it);
1603 }
Here is the caller graph for this function:

◆ Remove() [2/2]

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

Definition at line 1587 of file StdMeshMaterial.cpp.

1588 {
1589  if(update) update->Add(&*iter);
1590  Iterator next_iter = iter;
1591  ++next_iter;
1592  Materials.erase(iter.iter_);
1593  return next_iter;
1594 }

Friends And Related Function Documentation

◆ StdMeshMaterialUpdate

friend class StdMeshMaterialUpdate
friend

Definition at line 519 of file StdMeshMaterial.h.


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