Skip to content

Commit

Permalink
WIP render based on material
Browse files Browse the repository at this point in the history
Fix transparent objects not visible
Fix requiring copies for iteration (because of const)
  • Loading branch information
enginmanap committed Dec 23, 2024
1 parent 71f987d commit b35b1e0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Occlusion/RenderList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void RenderList::addMeshMaterial(const std::shared_ptr<const Material> &material
meshIterator->second.isAnimated = meshIterator->second.isAnimated || model->isAnimated();
meshIterator->second.boneTransforms = model->getBoneTransforms();
materialIterator->second.maxDepthPerMesh[meshAsset] = std::max(materialIterator->second.maxDepthPerMesh[meshAsset], maxDepth);//This is the max depth of this material
this->materialRenderPriorityMap.clear();//Why? because we don't know if we need to sort the list again
materialIterator->second.meshRenderPriorityMap.clear();//Why? because we don't know if we need to sort the list again
maxDepthPerMaterial[material] = std::max(maxDepthPerMaterial[material], maxDepth);
materialRenderPriorityMap.clear();//Why? because we don't know if we need to sort the list again
}
Expand Down
26 changes: 9 additions & 17 deletions src/Occlusion/RenderList.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ struct PerMeshRenderInformation {
class RenderList {
struct PerMaterialRenderInformation {
class perMaterialIterator {
PerMaterialRenderInformation& perMaterialRenderInformation;
const PerMaterialRenderInformation& perMaterialRenderInformation;
std::multimap<float, std::shared_ptr<MeshAsset>>::const_reverse_iterator it;
bool end;
public:
explicit perMaterialIterator(PerMaterialRenderInformation& perMaterialRenderInformation) : perMaterialRenderInformation(perMaterialRenderInformation), end(false) {
explicit perMaterialIterator(const PerMaterialRenderInformation& perMaterialRenderInformation) : perMaterialRenderInformation(perMaterialRenderInformation), end(false) {
if (perMaterialRenderInformation.meshRenderPriorityMap.empty()) {
for (auto &materialEntry : perMaterialRenderInformation.maxDepthPerMesh) {
perMaterialRenderInformation.meshRenderPriorityMap.insert(std::make_pair(materialEntry.second, materialEntry.first));
Expand All @@ -43,14 +43,6 @@ class RenderList {
}
}

perMaterialIterator& operator=(const perMaterialIterator& other) {
if (this != &other) {
perMaterialRenderInformation = other.perMaterialRenderInformation;
it = other.it;
end = other.end;
}
return *this;
}
bool isEnd() const {
return end;
}
Expand All @@ -71,7 +63,7 @@ class RenderList {
};
std::unordered_map<std::shared_ptr<MeshAsset>,PerMeshRenderInformation> meshesToRender;
std::unordered_map<std::shared_ptr<MeshAsset>, float> maxDepthPerMesh; //this map is created same time as meshes to render, but it is a pre transform container, as ordering by value not possible(or logical) in maps.
std::multimap<float, std::shared_ptr<MeshAsset>> meshRenderPriorityMap; //this map is created after first two values are created. It is just a sorted container to use sorting of materials
mutable std::multimap<float, std::shared_ptr<MeshAsset>> meshRenderPriorityMap; //this map is created after first two values are created. It is just a sorted container to use sorting of materials
std::unordered_map<std::shared_ptr<MeshAsset>, PerMeshRenderInformation>::iterator getOrCreateMeshEntry(const std::shared_ptr<MeshAsset> &meshAsset) {
std::unordered_map<std::shared_ptr<MeshAsset>, PerMeshRenderInformation>::iterator it = meshesToRender.find(meshAsset);
if (it == meshesToRender.end()) {
Expand All @@ -85,23 +77,23 @@ class RenderList {

public:
class RenderListIterator {
RenderList& renderList;
const RenderList& renderList;
bool end;
std::multimap<float, std::shared_ptr<const Material>>::reverse_iterator materialPriorityIt;
PerMaterialRenderInformation::perMaterialIterator* perMaterialIterator = nullptr;
mutable PerMaterialRenderInformation::perMaterialIterator* perMaterialIterator = nullptr;


public:
/**
* Provides an iterator that automatically iterates through materials and meshes, using depth information as ordering.
* Using this iterator is prone to iterator invalidation. Thats why we don't remove items while any of these are on the fly.
*/
explicit RenderListIterator(RenderList& renderList) : renderList(renderList), end(false)
explicit RenderListIterator(const RenderList& renderList) : renderList(renderList), end(false)
, materialPriorityIt(renderList.materialRenderPriorityMap.rbegin()) {
if (materialPriorityIt == renderList.materialRenderPriorityMap.rend()) {
end = true;
} else {
perMaterialIterator = new PerMaterialRenderInformation::perMaterialIterator(renderList.perMaterialMeshMap[materialPriorityIt->second]);
perMaterialIterator = new PerMaterialRenderInformation::perMaterialIterator(renderList.perMaterialMeshMap.at(materialPriorityIt->second));
}
}
~RenderListIterator() {
Expand Down Expand Up @@ -146,7 +138,7 @@ class RenderList {
private:
std::unordered_map<std::shared_ptr<const Material>, PerMaterialRenderInformation> perMaterialMeshMap; //Each mesh has its own render information
std::unordered_map<std::shared_ptr<const Material>, float> maxDepthPerMaterial; //this map is created same time as meshes to render, but it is a pre transform container, as ordering by value not possible(or logical) in maps.
std::multimap<float, std::shared_ptr<const Material>> materialRenderPriorityMap; //this map is created after first two values are created. It is just a sorted container to use sorting of materials
mutable std::multimap<float, std::shared_ptr<const Material>> materialRenderPriorityMap; //this map is created after first two values are created. It is just a sorted container to use sorting of materials

std::unordered_map<std::shared_ptr<const Material>, PerMaterialRenderInformation>::iterator getOrCreateMaterialEntry(const std::shared_ptr<const Material> &material) {
std::unordered_map<std::shared_ptr<const Material>, PerMaterialRenderInformation>::iterator it = perMaterialMeshMap.find(material);
Expand All @@ -160,7 +152,7 @@ class RenderList {
public:
void addMeshMaterial(const std::shared_ptr<const Material> &material, const std::shared_ptr<MeshAsset> &meshAsset, Model *model, uint32_t lod, float maxDepth);
void removeMeshMaterial(const std::shared_ptr<const Material> &material, const std::shared_ptr<MeshAsset> &meshAsset, uint32_t modelId);
RenderListIterator getIterator() {
RenderListIterator getIterator() const {
if (materialRenderPriorityMap.empty()) {
//first fill the map
for (auto &materialEntry : maxDepthPerMaterial) {
Expand Down
7 changes: 3 additions & 4 deletions src/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,7 @@ void World::renderDebug(const std::shared_ptr<GraphicsProgram>& renderProgram [[
debugDrawer->flushDraws();
}


void World::renderSingleRenderList(const std::shared_ptr<GraphicsProgram> &renderProgram, RenderList& renderList) const {
void World::renderSingleRenderList(const std::shared_ptr<GraphicsProgram> &renderProgram, const RenderList& renderList) const {
int diffuseMapAttachPoint = 1;
int ambientMapAttachPoint = 2;
int specularMapAttachPoint = 3;
Expand Down Expand Up @@ -925,7 +924,7 @@ void World::renderCameraByTag(const std::shared_ptr<GraphicsProgram> &renderProg
if (!VisibilityRequest::vectorComparator(renderListEntry.first, tags)) {
continue;
}
RenderList renderList = renderListEntry.second;
const RenderList& renderList = renderListEntry.second;
renderSingleRenderList(renderProgram, renderList);
}
}
Expand Down Expand Up @@ -981,7 +980,7 @@ void World::renderLight(unsigned int lightIndex, unsigned int renderLayer, const
if (!VisibilityRequest::vectorComparator(iterator.first, tags)) {
continue;
}
RenderList renderList = iterator.second;
const RenderList& renderList = iterator.second;
renderSingleRenderList(renderProgram, renderList);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ class World {
void renderSky(const std::shared_ptr<GraphicsProgram>& renderProgram, const std::string &cameraName [[gnu::unused]], const std::vector<HashUtil::HashedString> &tags [[gnu::unused]]) const;
void renderDebug(const std::shared_ptr<GraphicsProgram>& renderProgram, const std::string &cameraName [[gnu::unused]], const std::vector<HashUtil::HashedString> &tags [[gnu::unused]]) const;

void renderSingleRenderList(const std::shared_ptr<GraphicsProgram> &renderProgram, RenderList& renderList) const;
void renderSingleRenderList(const std::shared_ptr<GraphicsProgram> &renderProgram, const RenderList& renderList) const;

void renderPlayerAttachmentsRecursiveByTag(PhysicalRenderable *attachment, uint64_t renderTag, const std::shared_ptr<GraphicsProgram> &renderProgram,
std::vector<uint32_t> &alreadyRenderedModelIds) const;
Expand Down

0 comments on commit b35b1e0

Please sign in to comment.