Changed the structure of how RenderJobs are handled

This commit is contained in:
viktorljung
2016-01-11 17:01:17 +01:00
parent b8db80bc40
commit 3feee7fd35
7 changed files with 121 additions and 141 deletions
+57
View File
@@ -0,0 +1,57 @@
#ifndef ModelJob_h__
#define ModelJob_h__
#include <cstdint>
#include "../Common.h"
#include "../GLM.h"
#include "../Core/ComponentWrapper.h"
#include "Texture.h"
#include "Model.h"
#include "RenderJob.h"
#include "../Core/ResourceManager.h"
#include "Camera.h"
struct ModelJob : RenderJob
{
ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::Model::MaterialGroup texGroup, ComponentWrapper modelComponent)
: RenderJob()
{
Model = model;
TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0;
DiffuseTexture = texGroup.Texture.get();
NormalTexture = texGroup.NormalMap.get();
SpecularTexture = texGroup.SpecularMap.get();
StartIndex = texGroup.StartIndex;
EndIndex = texGroup.EndIndex;
Matrix = matrix;
Color = modelComponent["Color"];
Entity = modelComponent.EntityID;
};
unsigned int TextureID;
unsigned int ShaderID;
EntityID Entity;
glm::mat4 Matrix;
const Texture* DiffuseTexture;
const Texture* NormalTexture;
const Texture* SpecularTexture;
float Shininess = 0.f;
glm::vec4 Color;
const ::Model* Model = nullptr;
unsigned int StartIndex = 0;
unsigned int EndIndex = 0;
void CalculateHash() override
{
Hash = TextureID;
}
};
#endif
+32
View File
@@ -0,0 +1,32 @@
#ifndef RenderJob_h__
#define RenderJob_h__
#include <cstdint>
#include "../Common.h"
#include "../GLM.h"
#include "../Core/ComponentWrapper.h"
#include "RenderQueue.h"
struct RenderJob
{
friend class RenderQueue;
public:
float Depth;
protected:
uint64_t Hash;
virtual void CalculateHash() = 0;
bool operator<(const RenderJob& rhs)
{
return this->Hash < rhs.Hash;
}
};
#endif
+11 -96
View File
@@ -9,58 +9,16 @@
#include "../Core/Util/Rectangle.h" #include "../Core/Util/Rectangle.h"
#include "../Core/Entity.h" #include "../Core/Entity.h"
#include "Camera.h" #include "Camera.h"
#include "RenderJob.h"
#include "ModelJob.h"
class Model; class Model;
class Skeleton; class Skeleton;
class Texture; class Texture;
class RenderQueue;
//TODO: Render: Remove obsolete RenderJobs and fix standard values on variables. //TODO: Render: Remove obsolete RenderJobs and fix standard values on variables.
/*
struct RenderJob
{
friend class RenderQueue;
float Depth;
protected:
uint64_t Hash;
virtual void CalculateHash() = 0;
bool operator<(const RenderJob& rhs)
{
return this->Hash < rhs.Hash;
}
};
struct ModelJob : RenderJob
{
unsigned int ShaderID = 0;
unsigned int TextureID = 0;
EntityID Entity;
glm::mat4 Matrix;
const Texture* DiffuseTexture;
const Texture* NormalTexture;
const Texture* SpecularTexture;
float Shininess = 0.f;
glm::vec4 Color;
const Model* Model = nullptr;
unsigned int StartIndex = 0;
unsigned int EndIndex = 0;
// Animation
Skeleton* Skeleton = nullptr;
bool NoRootMotion = true;
std::string AnimationName;
double AnimationTime = 0;
void CalculateHash() override
{
Hash = TextureID;
}
};
struct TransparentModelJob : RenderJob struct TransparentModelJob : RenderJob
{ {
@@ -123,66 +81,23 @@ struct PointLightJob : RenderJob
Hash = 0; Hash = 0;
} }
}; };
*/
class RenderQueue
{
public:
template <typename T>
void Add(T &job)
{
job.CalculateHash();
Jobs.push_back(std::shared_ptr<T>(new T(job)));
m_Size++;
}
void Sort()
{
Jobs.sort();
}
void Clear()
{
Jobs.clear();
m_Size = 0;
}
int Size() const { return m_Size; }
std::list<std::shared_ptr<RenderJob>>::const_iterator begin()
{
return Jobs.begin();
}
std::list<std::shared_ptr<RenderJob>>::const_iterator end()
{
return Jobs.end();
}
std::list<std::shared_ptr<RenderJob>> Jobs;
private:
int m_Size = 0;
};
struct RenderScene struct RenderScene
{ {
RenderQueue Forward; ::Camera* Camera;
RenderQueue Lights; std::list<std::shared_ptr<RenderJob>> ForwardJobs;
Camera* Camera; std::list<std::shared_ptr<RenderJob>> LightJobs;
void Clear() void Clear()
{ {
Forward.Clear(); ForwardJobs.clear();
Lights.Clear(); LightJobs.clear();
}
void Sort()
{
Forward.Sort();
Lights.Sort();
} }
}; };
class RenderFrame struct RenderFrame
{ {
public: public:
+2 -1
View File
@@ -11,6 +11,7 @@
#include "../Core/EKeyDown.h" #include "../Core/EKeyDown.h"
#include "../Input/EInputCommand.h" #include "../Input/EInputCommand.h"
#include "Camera.h" #include "Camera.h"
#include "ModelJob.h"
class RenderSystem : public ImpureSystem class RenderSystem : public ImpureSystem
@@ -48,7 +49,7 @@ private:
glm::mat4 m_ProjectionMatrix; glm::mat4 m_ProjectionMatrix;
glm::mat4 ModelMatrix(EntityID entity); glm::mat4 ModelMatrix(EntityID entity);
void FillModels(RenderQueue* renderQueue); void FillModels(std::list<std::shared_ptr<RenderJob>>& jobs);
EventRelay<RenderSystem, Events::InputCommand> m_EInputCommand; EventRelay<RenderSystem, Events::InputCommand> m_EInputCommand;
bool OnInputCommand(const Events::InputCommand& e); bool OnInputCommand(const Events::InputCommand& e);
+1 -2
View File
@@ -32,9 +32,8 @@ void DrawScenePass::Draw(RenderFrame& rf)
DrawScenePassState state; DrawScenePassState state;
for (auto scene : rf.RenderScenes) { for (auto scene : rf.RenderScenes) {
scene->Forward.Jobs.sort(DrawScenePass::DepthSort);
for (auto &job : scene->Forward) { for (auto &job : scene->ForwardJobs) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job); auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
if (modelJob) { if (modelJob) {
GLuint ShaderHandle = m_BasicForwardProgram->GetHandle(); GLuint ShaderHandle = m_BasicForwardProgram->GetHandle();
+1 -1
View File
@@ -61,7 +61,7 @@ void PickingPass::Draw(RenderFrame& rf)
{ {
m_Camera = scene->Camera; m_Camera = scene->Camera;
for (auto &job : scene->Forward) { for (auto &job : scene->ForwardJobs) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job); auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
if (modelJob) { if (modelJob) {
+17 -41
View File
@@ -129,63 +129,39 @@ glm::mat4 RenderSystem::ModelMatrix(EntityID entity)
return modelMatrix; return modelMatrix;
} }
void RenderSystem::FillModels(RenderQueue* renderQueue) void RenderSystem::FillModels(std::list<std::shared_ptr<RenderJob>>& jobs)
{ {
auto models = m_World->GetComponents("Model"); auto models = m_World->GetComponents("Model");
if (models == nullptr) { if (models == nullptr) {
return; return;
} }
for (auto& modelC : *models) { for (auto& modelComponent : *models) {
bool visible = modelC["Visible"]; bool visible = modelComponent["Visible"];
if (!visible) { if (!visible) {
continue; continue;
} }
std::string resource = modelC["Resource"]; std::string resource = modelComponent["Resource"];
if (resource.empty()) { if (resource.empty()) {
continue; continue;
} }
glm::vec4 color = modelC["Color"];
Model* model = ResourceManager::Load<Model>(resource);
Model* model = ResourceManager::Load<::Model>(resource);
if (model == nullptr) { if (model == nullptr) {
model = ResourceManager::Load<Model>("Models/Core/Error.obj"); model = ResourceManager::Load<::Model>("Models/Core/Error.obj");
} }
glm::mat4 modelMatrix = ModelMatrix(modelComponent.EntityID);
glm::mat4 matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * modelMatrix);
for (auto texGroup : model->TextureGroups) { for (auto texGroup : model->TextureGroups) {
std::shared_ptr<ModelJob> modelJob = std::shared_ptr<ModelJob>(new ModelJob(model, m_Camera, matrix, texGroup, modelComponent));
if (color.a < 1.0f || texGroup.Transparency < 1.0f) { jobs.push_back(modelJob);
//transparent stuffs
TransparentModelJob job;
job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0;
job.DiffuseTexture = texGroup.Texture.get();
job.NormalTexture = texGroup.NormalMap.get();
job.SpecularTexture = texGroup.SpecularMap.get();
job.Model = model;
job.StartIndex = texGroup.StartIndex;
job.EndIndex = texGroup.EndIndex;
job.Matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * ModelMatrix(modelC.EntityID));
job.Color = color;
job.Entity = modelC.EntityID;
job.Depth = 10.f; //insert real viewspace depth here
renderQueue->Add(job);
} else {
ModelJob job;
job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0;
job.DiffuseTexture = texGroup.Texture.get();
job.NormalTexture = texGroup.NormalMap.get();
job.SpecularTexture = texGroup.SpecularMap.get();
job.Model = model;
job.StartIndex = texGroup.StartIndex;
job.EndIndex = texGroup.EndIndex;
job.Matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * ModelMatrix(modelC.EntityID));
job.Color = color;
job.Entity = modelC.EntityID;
renderQueue->Add(job);
}
} }
} }
} }
@@ -261,7 +237,7 @@ void RenderSystem::Update(World* world, double dt)
m_RenderFrame->Clear(); m_RenderFrame->Clear();
RenderScene* rs = new RenderScene(); RenderScene* rs = new RenderScene();
rs->Camera = m_Camera; rs->Camera = m_Camera;
FillModels(&rs->Forward); FillModels(rs->ForwardJobs);
m_RenderFrame->Add(*rs); m_RenderFrame->Add(*rs);
delete rs; delete rs;
} }