2020-09-21 22:10:50 +08:00
|
|
|
#pragma once
|
|
|
|
|
2024-05-03 22:15:08 +08:00
|
|
|
#include "base/Object.h"
|
2020-09-21 22:10:50 +08:00
|
|
|
#include "Texture.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2020-09-21 22:10:50 +08:00
|
|
|
|
2024-05-03 22:15:08 +08:00
|
|
|
class RenderTarget : public ax::Object
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
struct RenderBuffer
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
TextureBackend* texture = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
uint8_t level = 0; // level when attached to a texture
|
|
|
|
explicit operator bool() const { return texture != nullptr; }
|
2020-09-21 22:10:50 +08:00
|
|
|
};
|
|
|
|
typedef RenderBuffer ColorAttachment[MAX_COLOR_ATTCHMENT];
|
|
|
|
|
|
|
|
RenderTarget(bool defaultRenderTarget) : _defaultRenderTarget(defaultRenderTarget) {}
|
|
|
|
virtual ~RenderTarget()
|
|
|
|
{
|
|
|
|
for (auto colorItem : _color)
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(colorItem.texture);
|
|
|
|
AX_SAFE_RELEASE(_depth.texture);
|
|
|
|
AX_SAFE_RELEASE(_stencil.texture);
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
2020-09-22 16:32:17 +08:00
|
|
|
|
2022-06-24 14:18:48 +08:00
|
|
|
bool isDefaultRenderTarget() const { return _defaultRenderTarget; }
|
|
|
|
|
|
|
|
void setColorAttachment(ColorAttachment attachment)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2024-06-29 00:20:32 +08:00
|
|
|
for (int i = 0; i < MAX_COLOR_ATTCHMENT; ++i)
|
|
|
|
{
|
|
|
|
auto colorItem = _color[i];
|
|
|
|
if (colorItem.texture != attachment[i].texture || colorItem.level != attachment[i].level)
|
|
|
|
_dirtyFlags |= getMRTColorFlag(i);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(colorItem.texture);
|
2024-06-29 00:20:32 +08:00
|
|
|
}
|
2020-09-21 22:10:50 +08:00
|
|
|
memcpy(_color, attachment, sizeof(ColorAttachment));
|
|
|
|
for (auto colorItem : _color)
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(colorItem.texture);
|
2020-09-21 22:10:50 +08:00
|
|
|
};
|
2022-06-24 14:18:48 +08:00
|
|
|
|
2024-06-29 00:20:32 +08:00
|
|
|
void setColorAttachment(TextureBackend* attachment, int level = 0, int index = 0)
|
|
|
|
{
|
|
|
|
if (_color[index].texture != attachment || _color[index].level != level)
|
|
|
|
{
|
|
|
|
_dirtyFlags |= getMRTColorFlag(index);
|
|
|
|
AX_SAFE_RELEASE(_color[index].texture);
|
|
|
|
_color[index].texture = attachment;
|
|
|
|
_color[index].level = level;
|
|
|
|
AX_SAFE_RETAIN(_color[index].texture);
|
|
|
|
}
|
2022-06-24 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setDepthAttachment(TextureBackend* attachment, int level = 0)
|
2020-09-21 22:10:50 +08:00
|
|
|
{
|
2024-06-29 00:20:32 +08:00
|
|
|
if (_depth.texture != attachment || _depth.level != level)
|
|
|
|
{
|
|
|
|
_dirtyFlags |= TargetBufferFlags::DEPTH;
|
|
|
|
AX_SAFE_RELEASE(_depth.texture);
|
|
|
|
_depth.texture = attachment;
|
|
|
|
_depth.level = level;
|
|
|
|
AX_SAFE_RETAIN(_depth.texture);
|
|
|
|
}
|
2020-09-21 22:10:50 +08:00
|
|
|
};
|
2022-06-24 14:18:48 +08:00
|
|
|
void setStencilAttachment(TextureBackend* attachment, int level = 0)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2024-06-29 00:20:32 +08:00
|
|
|
if (_stencil.texture != attachment || _depth.level != level)
|
|
|
|
{
|
|
|
|
_dirtyFlags |= TargetBufferFlags::STENCIL;
|
|
|
|
AX_SAFE_RELEASE(_stencil.texture);
|
|
|
|
_stencil.texture = attachment;
|
|
|
|
_stencil.level = level;
|
|
|
|
AX_SAFE_RETAIN(_stencil.texture);
|
|
|
|
}
|
2022-06-24 14:18:48 +08:00
|
|
|
};
|
2024-06-29 00:20:32 +08:00
|
|
|
|
|
|
|
bool isDirty() const { return !!_dirtyFlags; }
|
2020-10-27 16:58:37 +08:00
|
|
|
|
2020-09-21 22:10:50 +08:00
|
|
|
ColorAttachment _color{};
|
|
|
|
RenderBuffer _depth{};
|
|
|
|
RenderBuffer _stencil{};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-27 16:58:37 +08:00
|
|
|
protected:
|
|
|
|
bool _defaultRenderTarget = false;
|
2024-06-29 00:20:32 +08:00
|
|
|
mutable TargetBufferFlags _dirtyFlags{};
|
2020-09-21 22:10:50 +08:00
|
|
|
};
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|