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

Member Enumeration Documentation

Enumerator
SMM_AcceptExisting 
SMM_ForceReload 

Definition at line 524 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 1663 of file StdMeshMaterial.cpp.

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

Referenced by CStdGL::PrepareMaterial().

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

References SMMS_FRAGMENT, SMMS_GEOMETRY, and SMMS_VERTEX.

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

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

Here is the caller graph for this function:

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:

void StdMeshMatManager::Clear ( )

Definition at line 1482 of file StdMeshMaterial.cpp.

Referenced by C4Game::Clear().

1483 {
1484  Materials.clear();
1485 
1486  Programs.clear();
1487  FragmentShaders.clear();
1488  VertexShaders.clear();
1489  GeometryShaders.clear();
1490 }

Here is the caller graph for this function:

Iterator StdMeshMatManager::End ( )
inline

Definition at line 562 of file StdMeshMaterial.h.

Referenced by C4DefGraphicsPtrBackup::Add().

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

Here is the caller graph for this function:

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

Definition at line 1604 of file StdMeshMaterial.cpp.

1605 {
1606  ShaderMap::const_iterator iter = FragmentShaders.find(StdCopyStrBuf(name));
1607  if(iter == FragmentShaders.end()) return nullptr;
1608  return iter->second.get();
1609 }
const StdMeshMaterialShader * StdMeshMatManager::GetGeometryShader ( const char *  name) const

Definition at line 1618 of file StdMeshMaterial.cpp.

1619 {
1620  ShaderMap::const_iterator iter = GeometryShaders.find(StdCopyStrBuf(name));
1621  if(iter == GeometryShaders.end()) return nullptr;
1622  return iter->second.get();
1623 }
const StdMeshMaterial * StdMeshMatManager::GetMaterial ( const char *  material_name) const

Definition at line 1579 of file StdMeshMaterial.cpp.

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

1580 {
1581  std::map<StdCopyStrBuf, StdMeshMaterial>::const_iterator iter = Materials.find(StdCopyStrBuf(material_name));
1582  if (iter == Materials.end()) return nullptr;
1583  return &iter->second;
1584 }

Here is the caller graph for this function:

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

Definition at line 1611 of file StdMeshMaterial.cpp.

1612 {
1613  ShaderMap::const_iterator iter = VertexShaders.find(StdCopyStrBuf(name));
1614  if(iter == VertexShaders.end()) return nullptr;
1615  return iter->second.get();
1616 }
std::set< StdCopyStrBuf > StdMeshMatManager::Parse ( const char *  mat_script,
const char *  filename,
StdMeshMaterialLoader loader 
)

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

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

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

Definition at line 1595 of file StdMeshMaterial.cpp.

Referenced by C4DefGraphicsPtrBackup::Add().

1596 {
1597  auto it = Materials.find(StdCopyStrBuf(name));
1598  if (it == Materials.end())
1599  return;
1600  if (update) update->Add(&it->second);
1601  Materials.erase(it);
1602 }

Here is the caller graph for this function:

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

Definition at line 1586 of file StdMeshMaterial.cpp.

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

Friends And Related Function Documentation

friend class StdMeshMaterialUpdate
friend

Definition at line 519 of file StdMeshMaterial.h.


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