Skip to content

Commit

Permalink
Adds initial command tree for batching render commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Argo15 committed Oct 4, 2021
1 parent 9059cfb commit 2bd1988
Show file tree
Hide file tree
Showing 31 changed files with 874 additions and 63 deletions.
104 changes: 64 additions & 40 deletions AggroEngine/AggroEngine.vcxproj

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions AggroEngine/AggroEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,39 @@
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\RenderChain\OpenGLRenderHandle.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\CommandTree.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\InitializeGBuffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\DepthTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\DrawElements.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\FilterLayer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\CommandTreeItem.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\Command.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\InitializeGBufferCommand.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\EmptyCommand.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\DisableDepthCommand.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\DrawElementsCommand.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Resource\Image\CheckersImage.hpp">
Expand Down Expand Up @@ -982,6 +1015,45 @@
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\RenderChain\OpenGLRenderHandle.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\CommandTree.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\Layer.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\InitializeGBuffer.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\ShouldRender.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\DepthTest.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\DrawElements.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Layer\FilterLayer.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\CommandTreeItem.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\Command.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\InitializeGBufferCommand.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\EmptyCommand.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\DisableDepthCommand.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Subsystem\Graphics\Impl\OpenGL\CommandTree\Commands\DrawElementsCommand.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="ClassDiagram.cd" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ bool PerspectiveFrustrum::isSame(shared_ptr<Frustrum> other)
FrustrumCulling PerspectiveFrustrum::getCulling(shared_ptr<glm::vec3> points, int size, glm::mat4 &modelMatrix)
{
glm::mat4 modelView = m_viewMatrix * modelMatrix;
glm::vec3 *mvPoints = new glm::vec3[size];
shared_ptr<glm::vec3> mvPoints(new glm::vec3[size]);
for (int i = 0; i < size; i++)
{
mvPoints[i] = glm::vec3(modelView * glm::vec4(points.get()[i], 1.0));
mvPoints.get()[i] = glm::vec3(modelView * glm::vec4(points.get()[i], 1.0));
}

for (int j = 0; j < 6; j++)
{
bool allOutside = true;
for (int i = 0; i < size; i++)
{
allOutside = allOutside && m_planes.get()[j].distance(mvPoints[i]) > 0;
allOutside = allOutside && m_planes.get()[j].distance(mvPoints.get()[i]) > 0;
}
if (allOutside)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "CommandTree.hpp"

CommandTree::CommandTree()
{

}

void CommandTree::addLayer(shared_ptr<Layer> layer)
{
m_layers.push_back(layer);
}

void CommandTree::execute(RenderOptions &renderOptions, shared_ptr<RenderChain> renderChain)
{
vector<shared_ptr<RenderNode>> allNodes;
shared_ptr<RenderNode> currentNode = renderChain->getFirst();
while (currentNode)
{
allNodes.push_back(currentNode);
currentNode = currentNode->next();
}
_executeLayer(renderOptions, allNodes, 0);
}

void CommandTree::_executeLayer(RenderOptions &renderOptions, vector<shared_ptr<RenderNode>> &renderNodes, int layerIdx)
{
if (m_layers.size() <= layerIdx || renderNodes.size() == 0)
{
return;
}
shared_ptr<Layer> layer = m_layers[layerIdx];
shared_ptr<CommandTreeItem> commandItem = layer->getCommands(renderOptions, renderNodes);
vector<shared_ptr<Command>> commands = commandItem->getCommands();
for (int i = 0; i < commands.size(); i++)
{
commands[i]->executeCommand();
_executeLayer(renderOptions, commands[i]->getRenderNodes(), layerIdx + 1);
commands[i]->end();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <memory>
#include <vector>
#include "Layer/Layer.hpp"
#include "RenderOptions.hpp"
#include "RenderChain.hpp"
using namespace std;

/* This object aims to minimize the amount of duplicate work required for rendering.
* In particular, it trys to reduce the number of OpenGL state changes, but can also
* be used to reduce the number of expensive CPU operations such as matrix multiplication.
*
* An example tree as follows
*
* Disable Depth Test | false | true |
* Shader Binding | shdr_a | shdr_b | shdr_c | shdr_b | shdr_d |
* Texture Unit 0 Binding | tex1 tex4 tex23 | tex4 tex7 | ... | ... | ... |
* Texture Unit 1 Binding | ... | ... |
* Texture Unit 2 Binding | | |
* Matrix Uniforms | | |
* Matrial Uniforms | | |
* Draw Elements | | |
*
*
* A cost guide from NVIDIA here: https://i.stack.imgur.com/JgrSc.jpg
* Basically: Render Target > Program > ROP > Texture Bindings >
* Vertex Format > UBO Bindings > Vertex Bindings > Uniform Updates
*/
class CommandTree
{
private:
vector<shared_ptr<Layer>> m_layers;

void _executeLayer(RenderOptions &renderOptions, vector<shared_ptr<RenderNode>> &renderNodes, int layerIdx);

public:
CommandTree();

void addLayer(shared_ptr<Layer> layer);
void execute(RenderOptions &renderOptions, shared_ptr<RenderChain> renderChain);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "CommandTreeItem.hpp"

CommandTreeItem::CommandTreeItem()
{
}

void CommandTreeItem::addCommand(shared_ptr<Command> command, shared_ptr<RenderNode> renderNode)
{
shared_ptr<Command> existing;
for (int i = 0; i < m_commands.size(); i++)
{
if (command->equals(m_commands[i]))
{
existing = m_commands[i];
break;
}
}
if (!existing)
{
m_commands.push_back(command);
existing = command;
}
if (renderNode)
{
existing->addRenderNode(renderNode);
}
}

vector<shared_ptr<Command>> &CommandTreeItem::getCommands()
{
return m_commands;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <unordered_map>
#include "Commands/Command.hpp"

class CommandTreeItem
{
private:
vector<shared_ptr<Command>> m_commands;
//unordered_map<Command, shared_ptr<Command>, CommandHash> m_existingCommands;

public:
CommandTreeItem();

void addCommand(shared_ptr<Command> command, shared_ptr<RenderNode> renderNode);
vector<shared_ptr<Command>> &getCommands();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "Command.hpp"

Command::Command()
: m_renderNodes()
{
}

void Command::addRenderNode(shared_ptr<RenderNode> node)
{
m_renderNodes.push_back(node);
}

vector<shared_ptr<RenderNode>> &Command::getRenderNodes()
{
return m_renderNodes;
}

void Command::executeCommand()
{
}

void Command::end()
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <vector>
#include "RenderNode.hpp"
using namespace std;

enum CommandType {
EMPTY = 0,
INIT_GBUFFER = 1,
DISABLE_DEPTH = 2,
DRAW_ELEMENTS = 3
};

class Command
{
protected:
vector<shared_ptr<RenderNode>> m_renderNodes;

public:
Command();

void addRenderNode(shared_ptr<RenderNode> node);
vector<shared_ptr<RenderNode>> &getRenderNodes();

virtual void executeCommand();
virtual void end();
virtual bool equals(shared_ptr<Command> other) { return false; };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "DisableDepthCommand.hpp"
#include "OpenGL43Graphics.hpp"

DisableDepthCommand::DisableDepthCommand()
: Command()
{
}

void DisableDepthCommand::executeCommand()
{
glDisable(GL_DEPTH_TEST);
}

void DisableDepthCommand::end()
{
glEnable(GL_DEPTH_TEST);
}

bool DisableDepthCommand::equals(shared_ptr<Command> other)
{
DisableDepthCommand *cmd = static_cast<DisableDepthCommand *>(other.get());
return cmd->m_type == CommandType::DISABLE_DEPTH;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "Command.hpp"

class DisableDepthCommand : public Command
{
private:
CommandType m_type = CommandType::DISABLE_DEPTH;

public:
DisableDepthCommand();

virtual void executeCommand();
virtual void end();
virtual bool equals(shared_ptr<Command> other);
};
Loading

0 comments on commit 2bd1988

Please sign in to comment.