File: RenderCommandPool.cpp

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (74 lines) | stat: -rw-r--r-- 2,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "RenderCommandPool.h"
#include "RenderCommand.h"
#include "RenderStatistics.h"

namespace nCine
{
	RenderCommandPool::RenderCommandPool(std::uint32_t poolSize)
	{
		freeCommandsPool_.reserve(poolSize);
		usedCommandsPool_.reserve(poolSize);
	}

	RenderCommandPool::~RenderCommandPool() = default;

	RenderCommand* RenderCommandPool::Add()
	{
		return usedCommandsPool_.emplace_back(std::make_unique<RenderCommand>()).get();
	}

	RenderCommand* RenderCommandPool::Add(GLShaderProgram* shaderProgram)
	{
		RenderCommand* newCommand = Add();
		newCommand->GetMaterial().SetShaderProgram(shaderProgram);
		return newCommand;
	}

	RenderCommand* RenderCommandPool::Retrieve(GLShaderProgram* shaderProgram)
	{
		RenderCommand* retrievedCommand = nullptr;

		for (std::uint32_t i = 0; i < freeCommandsPool_.size(); i++) {
			std::uint32_t poolSize = std::uint32_t(freeCommandsPool_.size());
			std::unique_ptr<RenderCommand>& command = freeCommandsPool_[i];
			if (command && command->GetMaterial().GetShaderProgram() == shaderProgram) {
				retrievedCommand = command.get();
				usedCommandsPool_.push_back(std::move(command));
				command = std::move(freeCommandsPool_[poolSize - 1]);
				freeCommandsPool_.pop_back();
				break;
			}
		}

#if defined(NCINE_PROFILING)
		if (retrievedCommand) {
			RenderStatistics::AddCommandPoolRetrieval();
		}
#endif
		return retrievedCommand;
	}

	RenderCommand* RenderCommandPool::RetrieveOrAdd(GLShaderProgram* shaderProgram, bool& commandAdded)
	{
		RenderCommand* retrievedCommand = Retrieve(shaderProgram);

		commandAdded = false;
		if (retrievedCommand == nullptr) {
			retrievedCommand = Add(shaderProgram);
			commandAdded = true;
		}

		return retrievedCommand;
	}

	void RenderCommandPool::Reset()
	{
#if defined(NCINE_PROFILING)
		RenderStatistics::GatherCommandPoolStatistics(std::uint32_t(usedCommandsPool_.size()), std::uint32_t(freeCommandsPool_.size()));
#endif
		for (std::unique_ptr<RenderCommand>& command : usedCommandsPool_) {
			freeCommandsPool_.push_back(std::move(command));
		}
		usedCommandsPool_.clear();
	}
}