1#include "RenderTarget.h"
3#include "Rendering/ITextures.h"
4#include "Rendering/IRenderTargets.h"
5#include "Rendering/ISwapChain.h"
7#include "Tasks/RenderTask.h"
10#include "RenderTexture.h"
12#include "Foundation/HashSequence.h"
13#include "Foundation/Logging/Logger.h"
20void Cogs::Core::RenderTarget::update(Renderer * renderer)
22 auto device = renderer->getDevice();
23 auto targets = device->getRenderTargets();
25 if (!checkUpdate())
return;
29 size_t textureCount = textures.size();
34 if (textureCount == 1) {
37 if(textures[0]->isOverride()) {
39 RenderTexture* texture = textures[0];
40 for (
size_t i = 0; i < static_cast<size_t>(texture->count); ++i) {
42 renderTargetHandles[i] = targets->createRenderTarget(&texture->textureHandles[i], 1);
43 targets->annotate(renderTargetHandles[i], getName().
to_string() +
" color target " + std::to_string(i));
45 depthTargetHandles[i] = depth ? targets->createDepthStencilTarget(renderTargetHandles[i], depth->textureHandle) : targets->createDepthStencilTarget(renderTargetHandles[i]);
46 targets->annotate(depthTargetHandles[i], getName().
to_string() +
" depth target " + std::to_string(i));
49 renderTargetHandle = renderTargetHandles[0];
50 depthTargetHandle = depthTargetHandles[0];
51 width = texture->description.width;
52 height = texture->description.height;
53 samples = texture->description.samples;
58 if (textures[0]->swapChain) {
76 if (textureCount == 0) {
81 else if (textureCount == 1) {
83 size_t levelViews = std::max(1u, std::min(textures[0]->description.levels,
static_cast<uint32_t
>(levels)));
85 RenderTargetViewDescription view = {
86 .texture = textures[0]->textureHandle,
87 .layerIndex = layerIndex,
88 .numLayers = numLayers,
91 mipLevelViews.resize(levelViews);
92 for (
size_t l = 0; l < levelViews; l++) {
93 view.levelIndex = uint8_t(l);
95 if (1 < multiViewCount) {
96 multiViewDesc.baseIndex = multiViewBaseIndex;
97 multiViewDesc.count = multiViewCount;
98 multiViewDesc.samples = multiViewSamples;
99 mipLevelViews[l] = targets->createRenderTarget(&view, 1, multiViewDesc);
102 mipLevelViews[l] = targets->createRenderTarget(&view, 1);
104 targets->annotate(mipLevelViews[l], getName().to_string() +
" view " + std::to_string(l));
106 renderTargetHandle = mipLevelViews[0];
113 std::vector<Cogs::TextureHandle> handles;
114 for (
auto& texture : textures) {
116 handles.push_back(texture->textureHandle);
120 if (!handles.empty()) {
121 mipLevelViews.resize(1);
122 mipLevelViews[0] = targets->createRenderTarget(handles.data(), handles.size());
123 targets->annotate(mipLevelViews[0], getName().to_string());
124 renderTargetHandle = mipLevelViews[0];
126 else if (textures.size() == 1) {
127 if (textures[0]->swapChain) {
128 renderTargetHandle = textures[0]->swapChain->getRenderTarget();
135 DepthStencilViewDescription depthTextureView = {};
136 depthTextureView.texture = depth->textureHandle;
137 depthTextureView.layerIndex = uint16_t(depthLayerIndex);
138 depthTextureView.numLayers = 1;
139 if (multiViewDesc.count) {
140 depthTargetHandle = targets->createDepthStencilTarget(renderTargetHandle, depthTextureView, multiViewDesc);
143 depthTargetHandle = targets->createDepthStencilTarget(renderTargetHandle, depthTextureView);
145 targets->annotate(depthTargetHandle, getName().to_string());
151 if (textures.size()) {
152 width = textures[0]->description.width;
153 height = textures[0]->description.height;
154 samples = textures[0]->description.samples;
157 width = depth->description.width;
158 height = depth->description.height;
159 samples = depth->description.samples;
164void Cogs::Core::RenderTarget::release(Renderer * renderer)
166 if ((textures.size() == 1) && textures[0]->swapChain) {
171 auto device = renderer->getDevice();
172 auto renderTargets = device->getRenderTargets();
175 renderTargets->releaseDepthStencilTarget(depthTargetHandle);
179 if (!mipLevelViews.empty()) {
180 for (
auto & h : mipLevelViews) {
182 renderTargets->releaseRenderTarget(h);
185 mipLevelViews.clear();
188 renderTargets->releaseRenderTarget(renderTargetHandle);
196 for (
auto & t : textures) {
197 if (t->getName() == name)
return t->textureHandle;
203bool Cogs::Core::RenderTarget::checkUpdate()
205 if (isOverride())
return false;
207 size_t generationHash =
hash();
208 for (
auto & t : textures) {
209 generationHash =
hash(t->getGeneration(), generationHash);
212 generationHash =
hash(depth->getGeneration(), generationHash);
214 generationHash =
hash(
static_cast<uint32_t
>(this->levels), generationHash);
216 if (generationHash != this->generationHash) {
217 this->generationHash = generationHash;
virtual int getWidth() const =0
Returns the pixel width of this swap chain.
virtual int getHeight() const =0
Returns the pixel height of this swap chain.
virtual const RenderTargetHandle & getRenderTarget() const =0
Returns the render target managed by this swap chain.
virtual const DepthStencilHandle & getDepthStencil() const =0
Returns the depth stencil managed by this swap chain.
virtual int getSamples() const =0
Returns the number of samples this swap chain has.
Log implementation class.
std::string to_string() const
String conversion method.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
static const Handle_t NoHandle
Represents a handle to nothing.
Describes multiview framebuffer layout.